在声明期间将值初始化为二维向量

发布于 2025-01-06 15:20:03 字数 1527 浏览 0 评论 0原文

我已经像这样声明了我的二维数组。但由于

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぶ宁プ宁ぶ 2025-01-13 15:20:03

您不能同时构造函数调用列表初始化,坚持使用一个,例如:(

vector<vector<int>> plain{{23,43,45,56},
                             {67,85,13,59},
                             {48,23,9,57},
                             {24,52,90,12}};

您可以省略列表初始化的= .)

关于您的编辑:嗯,错误解释了一切,阅读它会有所帮助。您需要 C++11 模式来进行列表初始化。如果您不希望这样,则必须使用 ctor 进行复制以准备 2D 数组,然后在构造后填充它。

You can't have both a constructor call and a list initialization, stick to one, e.g.:

vector<vector<int>> plain{{23,43,45,56},
                             {67,85,13,59},
                             {48,23,9,57},
                             {24,52,90,12}};

(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.

夏雨凉 2025-01-13 15:20:03

尝试使用参数显式调用构造函数。

try calling constructor explicitly with the arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文