错误:无法使用初始值设定项列表初始化非聚合类型 'vector<int>'

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

本文关键字:类型 gt vector 初始化 int lt 列表 错误      更新时间:2023-10-16

我是c++的初学者,每次我运行vector<int> nums = {2, 5, 3, 7, 1};时,它都会给我错误:非可逆类型向量不能用初始化列表初始化。

你能告诉我为什么吗?

谢谢,

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

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

If command not found: gcc++ try

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

如果你使用的是VS code,那么进入settings。json,添加"化c++ 17","-stdlib = libc + +";在args数组中。

附上设置的截图。参考Json