问题来源
当试图通过列表初始化 vector 时遇到的报错:
error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
vector<int> num = {2, 7, 11, 15};
编译环境
- m2 mac
- vscode + code runner 插件
解决问题
可能是 mac 芯片/系统 或者 code runner 参数问题,所以先找到 code runner 插件的配置设置,找到 cpp 文件的启动参数,下面省略:
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o -std=c++11 -g $fileNameWithoutExt && $dir$fileNameWithoutExt",
"zig": "zig run",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
将其中的 cpp (不是 c) 改成这样,加入 c++11 支持就可以了:
"cpp": "cd $dir && g++ $fileName -g -Wall -std=c++11 -o $fileNameWithoutExt && $dir$fileNameWithoutExt",