【问题标题】:How to create a Function that return any array of structs如何创建一个返回任何结构数组的函数
【发布时间】:2014-02-22 18:45:10
【问题描述】:

我知道必须有一种更好、更简单的方法来做我想做的事情,所以我很抱歉我缺乏编程知识。当我调用函数 printStructValue 时,我的程序崩溃了。我已经离开 cmets 来解释我的思考过程。

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

【问题讨论】:

  • 如果您从 C++ 开始,模板不是一个简单的起点。可能你需要在墙壁和地板之间有一个共同的基类?但是模板似乎不需要。

标签: c++ arrays templates struct return


【解决方案1】:

你的returnAnyArray 函数必须返回一些东西,但类型也必须匹配。试试这个

template<typename T, typename U> 
auto returnAnyArray(int st, T t, U u) -> decltype(st == 1 ? t : u)
{
    return st == 1 ? t : u;
}

【讨论】:

  • 啊好的。我一直在寻找这样的东西。我试过了,但得到一个错误“在'->'令牌之前需要初始化声明符。”这是我的语法错误吗?
  • @user3341184 不。您只需要启用 C++11。将选项 -std=c++11 添加到您的程序中。
  • 对不起,我不知道该怎么做,所以我在网上寻找它。但我有点想找到其他可能的方法。有人发布然后删除了一个很酷的想法。他们放置了一个主结构,其中包含地板和墙壁共享的类似变量,然后将 returnAnyArray 设为指向主结构的指针。我想在更新到 c++11 之前看到该方法有效。但谢谢你,我也在研究你的答案。
  • @user3341184 TBH 你可能不应该使用这个函数。你为什么不直接做printStructValue(W, 7); printStructValue(F, 7)
  • 是的,我知道这一点。虽然,我想通过选择运行一个 for 循环,以将一个 int 值(1 或 2)发送到 printStructValue,因为选择数组中的内容发生了变化。
【解决方案2】:

您的模板函数 returnAnyArray 实际上并没有返回任何内容。因此, printStructValue 被传递了一个垃圾指针。我很惊讶编译器没有捕捉到这一点并打印警告或错误。也许是因为它是一个模板。

【讨论】:

    【解决方案3】:

    以 C++ 方式进行:

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

    当然,这也不是非常复杂,但这是一个开始。

    【讨论】:

      【解决方案4】:

      有些工作,但可能不是您所期望的?

      #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.
      
      }
      

      【讨论】:

      • 是的,我知道这一点。虽然,我想通过选择运行一个 for 循环,以将一个 int 值(1 或 2)发送到 printStructValue,因为选择数组中的内容发生了变化。
      猜你喜欢
      • 2012-09-26
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2020-05-31
      相关资源
      最近更新 更多