我使用示例 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 和括號之間的空格:
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 不是很熟悉,但是知道這些原因以及我是否應該關注它們會很棒)
uj5u.com熱心網友回復:
您是否配置了 c VS Code 擴展?
例如:
{
"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”
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/386177.html
