Vector c++ 98 error

Compile with the -std=c++11 compiler option at the end of the line in the makefile.

So for example:

g++ -ggdb -O0 -c ENiX_Chocky.cpp -std=c++11
g++ -ggdb -O0 -c ENiX_NLPTest.cpp -std=c++11
...

Then when you link, use the -std=c++11 option again:

g++ -ggdb -O0 ENiX_Chocky.cpp ENiX_NLPTest.cpp -o CLINLPTest.cpp -std=c++11

The error will immediately disappear.


Initialization used by you is called initializer list and it is supported c++11 onwards.

To ensure code is compiled, use C++11 or later -std option. Or in general, don't use C++98.

If you are using g++, please read: Compiling C++11 with g++


From comments OP is using codeblocks. You can use the following steps before hitting the compile button: (Source: How can I add C++11 support to Code::Blocks compiler?)

  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

The C++98 Standard does not support initializer lists to initialize standard containers.

Try to set appropriate compiler options to compile the code according to the C++ 2011 Standard.

Another approach is to add elements to the vector individually like

std::vector<int> v1;
v1.reserve( 3 );

v1.push_back( 4 );
v1.push_back( 3 );
v1.push_back( 5 );

Instead of the member function push_back you can use overloaded operator +=. For example

std::vector<int> v1;
v1.reserve( 3 );

v1 += 4;
v1 += 3;
v1 += 5;

Or to use an array like

const size_t N = 3;
int a[N] = { 4, 3, 5 };
std::vector<int> v1( a, a + N );

Tags:

C++

Vector