此问题在此处已有答案:
How do I use C++20 in vscode?(3个答案)
How to enable C17 support in VSCode C Extension(8个答案)
2天前关闭。
我尝试用C++中的括号初始化一个map,如下所示:
const unordered_map <int, int> prova =
{
{0,1},
{4,2}
};
如果我在VSCode上编译,我会一直得到error: non-aggregate type 'const unordered_map<int, int>' cannot be initialized with an initializer list
。
我在编译器参数配置字段中包含了-std=c++20
,并尝试了g++/clang/clang++/gcc等的每种组合,得到了相同的结果。
当我从终端编译时,使用g++ -std=c++20 -o prova prova.cpp
时一切都很好,除非我省略了参数-std=c++20
,在这种情况下,我会得到与上面相同的错误。
我不得不承认我对c++的了解非常有限。据我所知,我的系统是最新的。g++ —-version
的完整输出如下
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 13.0.0 (clang-1300.0.29.30) Target: x86_64-apple-darwin20.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
.vscode/settings.json
文件为空,而.vscode/c_cpp_properties.json
看起来如下所示:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/g++",
"cStandard": "c17",
"cppStandard": "c++20",
"intelliSenseMode": "macos-clang-x64",
"compilerArgs": [
"-std=c++20"
]
}
],
"version": 4
}
这是.vscode/tasks.json
:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ compila il file attivo",
"command": "/usr/bin/clang++",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Attività generata dal debugger."
}
],
"version": "2.0.0"
}
您是否知道导致此问题的原因以及解决方法?
先谢谢你了
1条答案
按热度按时间sbdsn5lh1#
将
"-std=c++20"
添加到tasks.json
中的"args"
解决了该问题。