'string' was not declared in this scope解决方法

'string' was not declared in this scope

C++代码中声明定义了一个string类型的字符串对象,编译的时候,编译器却中断编译,并提示:'string' was not declared in this scope。这是什么意思呢?即,“string”类型并未此作用域内被声明过。比如下方的这段C++的代码。这是怎么回事呢?string不是C++内置的类吗?

#include <iostream>
int main(){
    string x = "abc";
    cout << x << endl;
    return 0;
}

原因

C++中的string类是命名空间std中的类,使用string类时,需要显式地通过"using namespace std;"的语句使用该命名空间。

解决方法

显示地添加“using namespace std;”,注意语句后面的“;”,而且一般写在include语句的下方,比如:

#include <iostream>
using namespace std;

免责声明:内容仅供参考,不保证正确性!


全栈后端 / C++教程 :
















Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.