C++ 错误:无法使用初始化列表初始化非聚合类型“vector<int>”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39022787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:48:11  来源:igfitidea点击:

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

c++vector

提问by Ian

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.

我是 C++ 的初学者,每次运行vector<int> nums = {2, 5, 3, 7, 1};它都会给我一个错误:无法使用初始化列表初始化非调整类型向量。

Can you tell me why?

你能告诉我为什么吗?

Thanks,

谢谢,

回答by Dhruv Sehgal

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

g++ -std=c++11 <filename>编译时使用。

回答by Mote Zart

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

使用上面 Druhv Sehgal 的回答,这对我在 mac 上有用

If command not found: gcc++try

如果command not found: gcc++尝试

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

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