在声明期间将值初始化为二维向量
我已经像这样声明了我的二维数组。但由于
vector <vector <int> > plain(vector <int>(4,0)) = {{23,43,45,56},
{67,85,13,59},
{48,23,9,57},
{24,52,90,12}};
rijndael.cpp:12:51: error: expected ‘,’ or ‘;’ before ‘=’ token
rijndael.cpp:57:1: error: expected ‘}’ at end of input
所有先前的分号都已正确分配而出现错误。 你能告诉我这个声明哪里出了问题吗?
我删除了构造函数调用,现在我的声明是
vector <vector <int> > plain/*(vector <int>(4,0))*/ = {{23,43,45,56},
{67,85,13,59},
{48,23,9,57},
{24,52,90,12}};
但现在的错误是
rijndael.cpp:15:19: error: in C++98 ‘plain’ must be initialized by constructor, not by ‘{...}’
rijndael.cpp:15:19: error: deducing from brace-enclosed initializer list requires #include <initializer_list>
rijndael.cpp:15:19: error: deducing from brace-enclosed initializer list requires #include <initializer_list>
rijndael.cpp:15:19: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
rijndael.cpp:15:19: error: could not convert ‘{{23, 43, 45, 56}, {67, 85, 13, 59}, {48, 23, 9, 57}, {24, 52, 90, 12}}’ to ‘std::vector<std::vector<int> >’
如何纠正这个问题? 非常感谢任何帮助。
I have declared my two dimensional array like this. But getting an error due to that
vector <vector <int> > plain(vector <int>(4,0)) = {{23,43,45,56},
{67,85,13,59},
{48,23,9,57},
{24,52,90,12}};
rijndael.cpp:12:51: error: expected ‘,’ or ‘;’ before ‘=’ token
rijndael.cpp:57:1: error: expected ‘}’ at end of input
All prior semi colons have been properly assigned.
Can you tell me where I'm going wrong in this declaration ?
I removed the constructor call and my declaration now is
vector <vector <int> > plain/*(vector <int>(4,0))*/ = {{23,43,45,56},
{67,85,13,59},
{48,23,9,57},
{24,52,90,12}};
But the error now is
rijndael.cpp:15:19: error: in C++98 ‘plain’ must be initialized by constructor, not by ‘{...}’
rijndael.cpp:15:19: error: deducing from brace-enclosed initializer list requires #include <initializer_list>
rijndael.cpp:15:19: error: deducing from brace-enclosed initializer list requires #include <initializer_list>
rijndael.cpp:15:19: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
rijndael.cpp:15:19: error: could not convert ‘{{23, 43, 45, 56}, {67, 85, 13, 59}, {48, 23, 9, 57}, {24, 52, 90, 12}}’ to ‘std::vector<std::vector<int> >’
How do I rectify this ?
Any help is very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能同时构造函数调用和列表初始化,坚持使用一个,例如:(
您可以省略列表初始化的
=
.)关于您的编辑:嗯,错误解释了一切,阅读它会有所帮助。您需要 C++11 模式来进行列表初始化。如果您不希望这样,则必须使用 ctor 进行复制以准备 2D 数组,然后在构造后填充它。
You can't have both a constructor call and a list initialization, stick to one, e.g.:
(You can omit the
=
for list initialization.)On your edit: Well, the error explains it all, reading it would help. You need C++11 mode for list initialization. If you don't want that, you'll have to copy with the ctor to prepare the 2D array and then fill it in after construction.
尝试使用参数显式调用构造函数。
try calling constructor explicitly with the arguments.