I am a novice, learning from the book Straustrup "Programming. Principles and practice using C ++". I got to the topic "Vectors" and decided to try to write this construction in the program:

vector<int> v = {5, 7, 9, 4, 6, 8}; 

What the compiler cursed:

in c ++ 98 must be initialized by constructor, not by '{...}'

Actually, the question is: how to solve this error?

  • I use the GCC compiler and Code :: Blocks 10.05 - niki4iko
  • one
    You are trying to construct your vector using the initialization list constructor ( ru.cppreference.com/w/cpp/utility/initializer_list ), which is not supported by the old compiler with ++ 98. Connect with ++ 11 (in different ide this is done so google it) - bronstein87
  • 2
    Either switch the compiler to C ++ 11 mode, or stop using C ++ 11 specific constructs. - AnT

1 answer 1

Initialization lists for containers appeared in C ++ 11. The compiler reports that you are now in C ++ 98 mode.

You should write like this:

 vector<int> v; v.push_back(5); v.push_back(7); v.push_back(9); v.push_back(6); v.push_back(8); 

Or enable C ++ mode 11.

To be honest, I have never seen Code :: Blocks, but Google claims that you need to do this: enter image description here

  • one
    Thank you very much for your reply! He helped, I turned on the C ++ 11 mode. But now a new error has come out: "no matching function for call to 'Vector <int> :: Vector (<brace-enclosed initializer list>)": p. Could you help with this error? Either I google badly, or just my hands are crooked, I can't find it ( - niki4iko
  • @ niki4iko, exactly Vector ? With a capital letter? - yrHeTaTeJlb
  • Yes, with a big one. But I wrote in the code with a little one, just as it is written in the topic - niki4iko
  • one
    @ niki4iko, maybe you have too old gcc. The online compiler (GCC 4.9.2) normally compiles such code. - yrHeTaTeJlb
  • Yes, the problem was this. Thank you so much again! It is a pity that the rating is not enough to give you a plus: ( - niki4iko