Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing values to two dimensional vectors during declaration

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.

like image 645
Sharat Chandra Avatar asked May 27 '26 10:05

Sharat Chandra


1 Answers

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.

like image 164
Xeo Avatar answered May 30 '26 06:05

Xeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!