Search code examples
c++vector

expected ';' at end of declaration /vector /c++


When I try to initialize a vector of ints, I always get this error:

expected ';' at end of declaration

I used the original code from "C++ Primer":

vector<int> v{1,2,3,4,5,6,7,8,9};

and

$ g++ -o test test.cpp

I think this is a silly question to ask, but I am sure that there is a ;. I cannot manage to find an answer.


Solution

  • g++ assumes C++03 by default, and the syntax you're trying to use came in C++11. Change the compilation line to:

    $ g++ -std=c++11 -o test test.cpp
    

    Or, as I'd personally prefer:

    $ g++ -Wall -Werror -pedantic -std=c++1y -o test test.cpp 
    

    :)

    Note: whether you'd use c++0x, c++11, or c++1y (and possibly c++14) depends mostly on the compiler version, as those were introduced in succesion.