我在eclipse上有一个c++项目,我可以在主文件中使用string类,但当我向该项目添加新文件时,我不能使用任何类。
我添加了文件如何:新建->源文件,并选择模板:默认c++模板源。
这是错误src/Common.cpp:8:17:错误:‘string’未在此作用域中声明
在src/AC.cpp中,一切正常
非常感谢
谢谢你,拉尔曼,我的简单代码是:
AC.cpp ->一切正常
include <iostream>
using namespace std;
int main()
{
string j = !World!;
cout << j << endl; // prints !World!
return 0;
}Common.cpp字符串src/Common.cpp:8:17:错误:未在此作用域中声明‘->’
#include <string>
void PrintTrace(string message)
{
string j = !World!;
cout << j << endl; // prints !World!
}这两个文件是同一个项目。
谢谢
发布于 2012-01-11 00:14:26
除了添加include指令#include <string> (如larsmans所述)之外,还必须使用它的完全限定名std::string来引用它。
如果只想使用string,请将using std::string;添加到使用它的每个.cpp源文件中(也可以添加using namespace std; but this is not recommended)。
https://stackoverflow.com/questions/8806331
复制相似问题