struct the_raw_data {
double data;
double other;
};
int testingReadFunctionsout() {
std::vector<the_raw_data> &thevector; /*Here is where the initialization needs to happen*/
return 0;
}
我收到以下错误:
main.cpp: In function ‘std::vector<the_raw_data>& readDataFromFileOut(std::__cxx11::string)’: main.cpp:108:29: error: ‘v’ declared as reference but not initialized std::vector<the_raw_data>& v;
答案 0 :(得分:4)
错误不言自明:
'v'声明为参考但未初始化
您已声明变量v
是参考,但它没有引用任何内容:
std::vector<the_raw_data> &thevector; // what is thevector pointing at? NOTHING!
您无法在C ++中使用未初始化的引用。引用只是另一个对象的别名,所以你必须初始化它以指向某个东西(实际上,将引用视为一个永远不能为NULL的指针,因为这是大多数编译器实际实现它的方式) ,例如:
std::vector<the_raw_data> &thevector = SomeOtherObject;
SomeOtherObject
是内存中其他地方的另一个std::vector<the_raw_data>
对象。
如果您希望v
成为其自己的实际std::vector<the_raw_data>
对象,只需删除变量声明中的&
:
std::vector<the_raw_data> thevector;