c++ - 在 C++ 和 Eclipse 上包含字符串类

标签 c++ eclipse class

我在 eclipse 上有一个 c++ 项目,我可以在主文件中使用字符串类,但是当我向项目添加一个新文件时,我不能使用任何类。

我如何添加文件:New-> Source file 并选择 Template: Defaul c++ template source。

这是错误 src/Common.cpp:8:17: error: ‘string’ was not declared in this scope

在 src/AC.cpp 中一切正常

非常感谢

谢谢 larsman 我的简单代码是:

AC.cpp -> 一切正常

include <iostream>
using namespace std;

int main()
{
string j = !!!Hello World!!!;
cout << j << endl; // prints !!!Hello World!!!
return 0;
}

Common.cpp -> src/Common.cpp:8:17: 错误:‘string’ 未在此范围内声明

#include <string>
void PrintTrace(string message)
{
    string j = !!!Hello World!!!;
cout << j << endl; // prints !!!Hello World!!!
}

这两个文件是同一个项目。

谢谢

最佳答案

#include <string>

您必须在所有使用 std::string 的模块中包含此 header 。

关于c++ - 在 C++ 和 Eclipse 上包含字符串类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8806331/

相关文章:

c++ - 用Lambda函数重新捕获变量是否合法?

c++ - 在不知道参数的情况下传递模板

java - 如何使用 main() 中的对象数组访问类私有(private)成员?

c++ - 出现错误 C2679 : binary '+=' : no operator found which takes a right-hand operand of type 'int' 的问题

c++ - 如何使用字符串和整数获取字符串?

c++ - 如何使队列上的推送和弹出成为原子操作,如何锁定这些操作?

android - 如何将 Mapsforge 库源导入 Eclipse

eclipse - 加载 Gradle 项目预览失败

java - PlatformUI.getWorkbench().getService(IEventBroker.class) 返回 null

java - 创建一个类的方法并在主类中调用它