我已经定义了一个结构:
struct sym{
int data_onel;
int data_two;
}
我有功能:
sym*& fillStruct(){
sym array_of_struct[size];
/.. fill array with data
return *array_of_struct;
}
我在其中创建结构数组并用数据填充结构。但是我想在另一个函数中返回这个 arr。例如
bool another(){
sym *next_array = fillStruct();
}
我希望它通过引用传递,这样它就不会被破坏,但它一直在抛出:
invalid initialization of reference of type 'Symbols*&' from expression of type 'Symbols'
我怎样才能从函数返回结构数组呢?有引用意义。
最佳答案
你的根本问题是你试图返回一个本地数组,无论你如何旋转它都不会工作。更好的方法是使用 vector ; vector 也可以在本地声明,但与您的数组不同,它可以返回。
vector<sym> fillStruct(){
vector<sym> array_of_struct;
/.. fill array with data
return array_of_struct;
}
bool another(){
vector<sym> next_array = fillStruct();
}
关于c++ - 返回对结构数组的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36112127/