[英]How to fix error ininitialize vector of struct
這是我的代碼。 (C ++ 98)
struct node
{
string name;
string help;
string action;
string pName;
string pHelp;
};
vector<node> commands {
node{"name1", "help1", "", "", ""},
node{"name2", "help2", "action2", "pname", "phelp"}
};
錯誤是
函數定義未聲明參數
您可能正在使用舊的編譯器,但正在閱讀新的教程或書籍。 gcc 5.4.0給出了以下內容:
test.cpp:12:27: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
vector <node> commands {
^
test.cpp:13:10: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
node{"name1","help1", "", "" , ""}, node{"name2", "help2","action2", "pname", "phelp"}
^
test.cpp:13:46: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
node{"name1","help1", "", "" , ""}, node{"name2", "help2","action2", "pname", "phelp"}
^
test.cpp:14:6: error: in C++98 ‘commands’ must be initialized by constructor, not by ‘{...}’
};
顯然,您要么至少必須使用c ++ 11,要么需要為帶有五個參數並使用舊樣式構造對象的node
提供構造函數。
除非您有非常特定的理由堅持使用c ++ 98,否則我會說轉到C ++ 11是最佳選擇。 否則,請遵循教C ++ 98的書籍或教程,或者至少描述C ++ 98中的不同之處以避免此類問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.