-1

Code:

#include <iostream> 
#include <vector> 
using namespace std; 

int main() 
{ 
    vector<int> myvector{ 1, 2, 3, 4, 5 };  
    for (auto it = myvector.begin(); 
         it != myvector.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
} 

Everytime I run vector , error message will say... "it must be initialized by constructor, not be'{...}'

And; could not convert '{1, 2, 3, 4, 5, 6}' from '' to std::vector"

What should I do?

  • 10
    Sounds like you need to [enable C++11](https://stackoverflow.com/q/18174988/10077). – Fred Larson Jun 05 '20 at 16:33
  • 1
    Initializer lists are a C++11 feature (I think). You need to change your project properties to use the relevant compiler flag (can't remember how to do this, sorry). Hopefully that sets you in the right direction – castro Jun 05 '20 at 16:34
  • 1
    Code::blocks is often shipped with an older compiler that understands C++11 but does not enable it by default. [This should be of help.](https://stackoverflow.com/questions/18174988/how-can-i-add-c11-support-to-codeblocks-compiler) You can also [update the compiler](https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2). Updating the compiler is a bit more complicated, but worth it in my books – user4581301 Jun 05 '20 at 16:37
  • 1
    I don't trust an ide for C++ that doesn't even enable C++11 in 2021 – bolov May 14 '21 at 08:47
  • 1
    dupe of [Vector c++ 98 error](https://stackoverflow.com/questions/36173078/vector-c-98-error) and others, found by searching for the quoted error – underscore_d May 14 '21 at 08:47

1 Answers1

1

You would have to change from -std=C++98 to about -std=-C++11 or more. This can be done in your compiler settings.

  1. Go to settings
  2. Click on compiler...
  3. on the compiler settings tab, you would find the options, so choose (check it within the checkbox) -stf=c++11 or upwards
  4. Click OK and run your program again.