30

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.

0

4 Answers 4

56

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.

Sign up to request clarification or add additional context in comments.

Comments

8

Your compiler by default does not support brace initialisation; this was added in C++11.

There's probably a command line argument you can use in your compiler, something along the lines of

-std=c++11

Comments

1

Open VS Code settings
search "executor map"
Under "Code-runner: Executor Map" -> click on "Edit in setting.json"
in "code-runner.executorMap"
replace
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
with
"cpp": "cd $dir && g++ -std=c++14 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"

Comments

0

I think u use code runner for build and start .cpp. U must go in setting this extension and change 'Executor Map'. Add for cpp: -std=c++17 -stdlib=li

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.