Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

vector

when I try to initialise a vector of int in c++, I always get the "expected ';' at end of declaration" error.

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 ";"... and can not manage to search an answer.. Thanks.

like image 271
spicyShoyo Avatar asked Jan 13 '15 14:01

spicyShoyo


1 Answers

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.

like image 110
Bartek Banachewicz Avatar answered Sep 28 '22 16:09

Bartek Banachewicz