expected ';' at end of declaration /vector /c++
Asked Answered
R

4

29

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.

Rexanna answered 13/1, 2015 at 14:9 Comment(0)
K
53

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.

Kadiyevka answered 13/1, 2015 at 14:12 Comment(0)
K
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

Krutz answered 13/1, 2015 at 14:11 Comment(0)
P
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"

Paynim answered 23/2 at 18:43 Comment(0)
F
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

Foresheet answered 12/11, 2023 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.