I use the sample helloworld program and get syntax errors that make no sense to me. The strange part is that the program runs just fine, but the red squiggles in the code bother me and I'd like to understand why those are happening.
Code
#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;
}
And this is the tasks.json file
{
"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
}
}
]
}
One of the underlined errors is the space between msg and the brackets:
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])
The next is at the colon in the for loop:
reference variable "word" requires an initializer
And the last one is the closing parenthesis of the for loop (after msg):
expected an expression
What's causing these errors, and how is the program still running? (I should mention that I'm not very familiar with C , but it would be great to know the reason for these and whether I should be concerned about them)
CodePudding user response:
Did you configure your c VS Code extension?
For example:
{
"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
}
The important bits are: "cStandard": "c11", "cppStandard": "c 17"