如何解决VSCODE / G++不识别默认值包括文件?
我是C ++编码的新手,几乎没有经验。我的问题是:我想使用字符串,所以我在代码中的包含中添加了#include,但是VSCODE告诉我标识符“字符串”是Undefinedc/c ++(20)
。我所做的工作已添加并修改了C_CPP_PROPERTIES.json文件VOR VSCODE:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
但这并不能解决问题。另外,我还确保安装了C ++,包括G ++编译器。在VSCODE中,我使用以下C ++插件/插件:C/C ++和C/C ++项目生成器(我实际上并不使用)。 这是我代码的相关部分:
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
string s;
return 0;
}
这是错误日志/调试消息:
g++ -std=c++17 -Wall -Wextra -g -Iinc -c src/main.cpp -o src/main.o
src/main.cpp: In function ‘int main(int, char**)’:
src/main.cpp:30:2: error: ‘string’ was not declared in this scope
30 | string s;
| ^~~~~~
src/main.cpp:30:2: note: suggested alternatives:
In file included from /usr/include/c++/9/iosfwd:39,
from /usr/include/c++/9/ios:38,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from src/main.cpp:1:
/usr/include/c++/9/bits/stringfwd.h:79:33: note: ‘std::string’
79 | typedef basic_string<char> string;
| ^~~~~~
In file included from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from src/main.cpp:1:
/usr/include/c++/9/string:67:11: note: ‘std::pmr::string’
67 | using string = basic_string<char>;
| ^~~~~~
解决方案:String s;
用std :: String s;
。
I am very new to coding in C++ and have little to no experience at all. My problem is: I wanted to use strings so i added #include to the includes in my code but VSCode tells me identifier "string" is undefinedC/C++(20)
. What I have done is added and modified a c_cpp_properties.json file vor VSCode:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
But that didn't solve the problem. Also, I have made sure that C++ is installed, including the g++ compiler. In VSCode I am using following C++ plugins / addons: C/C++ and C/C++ Project Generator (which I don't really use).
This is the relevant part of my code:
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
string s;
return 0;
}
And this is the error log / debug message:
g++ -std=c++17 -Wall -Wextra -g -Iinc -c src/main.cpp -o src/main.o
src/main.cpp: In function ‘int main(int, char**)’:
src/main.cpp:30:2: error: ‘string’ was not declared in this scope
30 | string s;
| ^~~~~~
src/main.cpp:30:2: note: suggested alternatives:
In file included from /usr/include/c++/9/iosfwd:39,
from /usr/include/c++/9/ios:38,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from src/main.cpp:1:
/usr/include/c++/9/bits/stringfwd.h:79:33: note: ‘std::string’
79 | typedef basic_string<char> string;
| ^~~~~~
In file included from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from src/main.cpp:1:
/usr/include/c++/9/string:67:11: note: ‘std::pmr::string’
67 | using string = basic_string<char>;
| ^~~~~~
Solution: Replace string s;
with std::string s;
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C ++字符串在
std
名称空间内“隐藏”。因此,编译器无法识别字符串
。您可以包括整个
std
namespace:对于专门的字符串,无需:
或键入
std :: string
而不是string
在初始化时字符串。 C ++标准库在编译器的包含路径中,默认情况下,您无需做任何额外的事情就能使用其标题。C++ strings are "hidden" inside the
std
namespace. The compiler doesn't recognizestring
because of that.You could either include the entire
std
namespace:Do this for specifically strings and nothing else:
or type
std::string
instead ofstring
when initializing strings. The C++ Standard Library is in the compiler's include path by default, you don't need to do anything extra to be able to use its headers.