Vector c++ 98 error

Viewed 30807

so ive been reading multiple (and resent) c++ books and learning about vectors and they all are telling me to define a vector like this:

 vector<int> v1 = {4 ,3 ,5}; 

however when i compile it (Im using gnu gcc compiler in codeblocks) it comes up with this error

in c++ 98 'v1' must be initialized by constructor not by '{...}' and i also get another one underneath that that sais: could not convert '{4, 3, 5}' from 'brace enclosed initializer list' to 'std::vector v1'

if you could help me it'd be much appreciated. And i did include the vector library.

5 Answers

Follow these steps if you are using Codeblocks:

1.Go to Toolbar -> Settings -> Compiler

2.In the "Selected compiler" drop-down menu, make sure "GNU GCC Compiler" is selected.

3.Below that, select the "compiler settings" tab and then the "compiler flags" tab underneath.

4.In the list below, make sure the box for "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]" is checked.

5.Click OK to save

Intilizer_list only supported on c++11 onwards.

Compile your program as below :

  1. If you are using c++11 features then follow -std=c++11 option;
  2. If you are using c++14 features then follow -std=c++14 option.

EX:

gcc Temp.cpp -std=c++11 -o out_put
gcc Temp.cpp -std=c++14 -o out_put
Related