如何解决VSCODE / G++不识别默认值包括文件?

发布于 2025-01-29 14:18:26 字数 2101 浏览 4 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2025-02-05 14:18:26

C ++字符串在std名称空间内“隐藏”。因此,编译器无法识别字符串

您可以包括整个std namespace:

using namespace std;

对于专门的字符串,无需:

using std::string;

或键入std :: string而不是string在初始化时字符串。 C ++标准库在编译器的包含路径中,默认情况下,您无需做任何额外的事情就能使用其标题。

C++ strings are "hidden" inside the std namespace. The compiler doesn't recognize string because of that.

You could either include the entire std namespace:

using namespace std;

Do this for specifically strings and nothing else:

using std::string;

or type std::string instead of string 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文