简体   繁体   English

如何修复错误初始化结构向量

[英]How to fix error ininitialize vector of struct

This is my code. 这是我的代码。 (C++98) (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"}
};

The error is 错误是

function definition does not declare parameters 函数定义未声明参数

You are probably using an old compiler but following new tutorial or book. 您可能正在使用旧的编译器,但正在阅读新的教程或书籍。 gcc 5.4.0 gives this: 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 ‘{...}’
      };

Which clearly says that you either have to use c++11 at least or need to provide a constructor for node that takes five parameters and use the old style to construct objects. 显然,您要么至少必须使用c ++ 11,要么需要为带有五个参数并使用旧样式构造对象的node提供构造函数。

Unless you have very specific reasons to stick to c++98, I would say that moving to C++11 is the best option. 除非您有非常特定的理由坚持使用c ++ 98,否则我会说转到C ++ 11是最佳选择。 Otherwise please follow books or tutorials that teach C++98 or at least describe how things were different in C++98 to avoid such problems. 否则,请遵循教C ++ 98的书籍或教程,或者至少描述C ++ 98中的不同之处以避免此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM