VSCode 和 C++:由于此错误而略有丢失
VSCode and C++: Slightly lost with this error
我使用示例 helloworld 程序并遇到对我来说毫无意义的语法错误。奇怪的是程序运行得很好,但代码中的红色波浪线让我很困扰,我想了解为什么会这样。
代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
这是 tasks.json 文件
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
带下划线的错误之一是 msg 和方括号之间的 space:
no instance of constructor "std::__1::vector<_Tp, _Allocator>::vector [with _Tp=std::__1::string, _Allocator=std::__1::allocator<std::__1::string>]" matches the argument list -- argument types are: (const char [6], const char [4], const char [6], const char [5], const char [8], const char [23])
下一个是 for 循环中的冒号:
reference variable "word" requires an initializer
最后一个是for循环的右括号(在msg之后):
expected an expression
是什么导致了这些错误,程序如何仍然 运行? (我应该提一下,我对 C++ 不是很熟悉,但如果知道这些原因以及我是否应该关注它们,那就太好了)
您是否配置了您的 C++ VS 代码扩展?
例如:
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
重要的位是:“cStandard”:“c11”,
“cppStandard”:“c++17”
我使用示例 helloworld 程序并遇到对我来说毫无意义的语法错误。奇怪的是程序运行得很好,但代码中的红色波浪线让我很困扰,我想了解为什么会这样。
代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
这是 tasks.json 文件
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
带下划线的错误之一是 msg 和方括号之间的 space:
no instance of constructor "std::__1::vector<_Tp, _Allocator>::vector [with _Tp=std::__1::string, _Allocator=std::__1::allocator<std::__1::string>]" matches the argument list -- argument types are: (const char [6], const char [4], const char [6], const char [5], const char [8], const char [23])
下一个是 for 循环中的冒号:
reference variable "word" requires an initializer
最后一个是for循环的右括号(在msg之后):
expected an expression
是什么导致了这些错误,程序如何仍然 运行? (我应该提一下,我对 C++ 不是很熟悉,但如果知道这些原因以及我是否应该关注它们,那就太好了)
您是否配置了您的 C++ VS 代码扩展?
例如:
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
重要的位是:“cStandard”:“c11”, “cppStandard”:“c++17”