这是我的代码。 (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"}
};
错误是
function definition does not declare parameters
最佳答案
您可能正在使用旧的编译器,但正在遵循新的教程或书籍。 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 中的情况有何不同,以避免此类问题。
关于c++ - 如何修复结构体初始化 vector 时的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53993212/