我在 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/