简体   繁体   中英

How to create a Function that return any array of structs

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");
}

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;
}

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.

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.

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM