2017-09-29 8 views
-2
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

それはすべてが私の頭の上に行ってきました、私は別の関数からそれを返していましたそれを変更するのを忘れてしまった。ありがとう。 – abeltre1

答えて

4

エラーが一目瞭然です:

‘v’ declared as reference but not initialized

あなたは参照である変数vを宣言したしかし、それは何も参照しません:

std::vector<the_raw_data> &thevector; // what is thevector pointing at? NOTHING! 

C++で初期化されていない参照を持つことはできません。参照は別のオブジェクトのエイリアスなので、ポインタを何かを指すように初期化する必要があります(実際には、ポインタをNULLにすることはできません。例えば:SomeOtherObjectは他の場所でメモリ内の別のstd::vector<the_raw_data>のオブジェクトである

std::vector<the_raw_data> &thevector = SomeOtherObject; 

。あなたはvは、独自の実際のstd::vector<the_raw_data>対象にしたい場合は

、ちょうど変数宣言に&を取り除く:

std::vector<the_raw_data> thevector; 
関連する問題