0

I know there has to be a better and easier way to do what I am trying to do so I'm sorry for my lack of knowledge of programming. My program crashes when I call the function printStructValue. I have left comments to explain my thought process.

#include <iostream>
#include <vector>
using namespace std;

struct selection   //vector array to tell me what is selected. ex:'w',5 is wall 5 
{    
  char c;
  int id;
}; vector<selection> Sel(20,selection());

struct walls       //struct to hold wall data
{
  int id;
  int x,y,z;
  int spriteState;
}; walls W[10];

struct floors     //struct to hold floor data
{
  int id;
  int x,y,z;
}; floors F[10];


template <typename T,typename U> 
T returnAnyArray(int st, T t,U u)  //function that returns any type passed
{
  if(st==1){t;}  //if st==1, then return the first, walls W
  if(st==2){u;}  //if st==2, then return the second, floors F
}

template <typename T> 
void printStructValue(T t, int d)  //print any struct value 
{ 
  cout<<"passed:"<<t[d].x<<endl; 
}

int main()
{  
  W[7].x=204;   //init value
  F[7].x= 73;   //init value

  //what I would like to happen is...  
  printStructValue( (returnAnyArray(1,W,F)),7);  //W is returned and passed so W[7].x gets printed.
  printStructValue( (returnAnyArray(2,W,F)),7);  //F is returned and passed so F[7].x gets printed.

  system("pause");
}
1
  • If you start with C++, template is not an easy starting point. Probably you need to have a common base class between walls and floors ? However template seems not needed. Commented Feb 22, 2014 at 18:57

4 Answers 4

1

Your returnAnyArray function has to return something, but the types also have to match. Try this

template<typename T, typename U> 
auto returnAnyArray(int st, T t, U u) -> decltype(st == 1 ? t : u)
{
    return st == 1 ? t : u;
}
Sign up to request clarification or add additional context in comments.

7 Comments

ah ok. I was looking for something like this. I tried it but got an error "expected init-declarator before '->' token." Is that a syntax error on my end?
@user3341184 Nope. You just need to enable C++11. Add the option -std=c++11 to your program.
sorry i wasnt sure how to do that so i was looking online for it. But i kind of wanted to find other possible ways. someone posted then deleted a cool idea. they put a master struct with the similar variables that floors and walls both share, then made returnAnyArray a pointer to the main struct. i wanted to see it that method worked before i update to c++11. but thank you i am researching your answer too.
@user3341184 TBH you probably shouldn't be using this function anyway. Why aren't you just doing printStructValue(W, 7); printStructValue(F, 7)?
Yes i knew about this. although, I wanted to run a for loop through the selection to send an int value (1 or 2) to the printStructValue since what is in the selection array changes.
|
0

Your template function, returnAnyArray, doesn't actually return anything. Hence, printStructValue is being passed a garbage pointer. I'm surprised the compiler didn't catch this and print a warning or error. Perhaps because it's a template.

Comments

0

Do it the C++ way:

struct structuralMember {
  virtual ~structualMember() { }
  virtual void printme(std::ostream& out) = 0;
};

struct walls : public structualMember {      
  int x;
  void printme(std::ostream& out) { out << x; }
  };

struct floors : public structuralMember {
  int x;
  void printme(std::ostream& out) { out << x; }
  };

Of course, that's not terribly sophisticated either, but it's a start.

Comments

0

Something working, but probably not what you are expecting ?

#include <iostream>
#include <vector>
using namespace std;

struct walls     //struct to hold wall data
{
  int id;
  int x,y,z;
  int spriteState;
}; 
walls W[10];

struct floors //struct to hold floor data
{
  int id;
  int x,y,z;
}; 
floors F[10];

void printStructValue(walls * t, int d)  //print any struct value 
{ 
  cout<<"passed:" << t[d].x<<endl; 
}

void printStructValue(floors * t, int d)  //print any struct value 
{ 
  cout<<"passed:"<< t[d].x<<endl; 
}

int main()
{  
  W[7].x=204;   //init value
  F[7].x= 73;   //init value

  //what I would like to happen is...  
  printStructValue( W,7);  //W is returned and passed so W[7].x gets printed.
  printStructValue( F,7);  //F is returned and passed so F[7].x gets printed.

}

1 Comment

Yes i knew about this. although, I wanted to run a for loop through the selection to send an int value (1 or 2) to the printStructValue since what is in the selection array changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.