Error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
Asked Answered
I

3

64

I am a beginner in c++ and every time I run vector<int> nums = {2, 5, 3, 7, 1}; it gives me the Error: non-arregrate type vector cannot be initialized with initializer list.

Can you tell me why?

Thanks,

Ium answered 18/8, 2016 at 16:0 Comment(7)
Most likely you need to turn on C++11 or higher.Nerty
Can you tell me how to turn it on on a mac?Ium
What compiler are you using and how are you compiling?Nerty
I am using gcc on the compilerIum
@ian just pass -std=c++11.Odontograph
it said command not found.Ium
You need to pass it as an option to the compiler, e.g. g++ -std=c++11 prog.cpp.Odontograph
F
110

Use g++ -std=c++11 <filename> when compiling.

Flossie answered 18/8, 2016 at 16:6 Comment(0)
C
10

Using Druhv Sehgal's answer above, this worked for me on mac

If command not found: gcc++ try

clang++ -std=c++11 <filename>

Castaneda answered 12/10, 2019 at 18:47 Comment(2)
Where can I read about -std flag? I read cs.cmu.edu/~cburch/211-sp96/gnu_devel/gpp.html , but didn't found that option.Sung
I found this gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html, which has some options. I've only ever used c++11 so I'm not expert. This looks good too gcc.gnu.org/projects/cxx-status.htmlCastaneda
J
3

If you are using VS code, then go to settings.json, Add "-std=c++17", "-stdlib=libc++" in the args array.

Attached the screenshot of the settings.json for reference

Jaela answered 22/7, 2023 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.