【发布时间】:2013-03-31 23:29:35
【问题描述】:
我正在尝试这样做:
#include <string>
class Medicine{
string name;
};
但它根本不起作用。我尝试右键单击项目-> 索引-> 搜索未解决的包含,它说:项目中未解决的包含(0 个匹配项)。它也不适用于 std::string 。我该怎么办?
【问题讨论】:
-
@David 它不工作。
我正在尝试这样做:
#include <string>
class Medicine{
string name;
};
但它根本不起作用。我尝试右键单击项目-> 索引-> 搜索未解决的包含,它说:项目中未解决的包含(0 个匹配项)。它也不适用于 std::string 。我该怎么办?
【问题讨论】:
您应该使用它所属的命名空间 (std) 完全限定 string:
#include <string>
class Medicine {
std::string name;
// ^^^^^
};
或使用using 声明:
#include <string>
using std::string; // <== This will allow you to use "string" as an
// unqualified name (resolving to "std::string")
class Medicine {
string name;
// ^^^^^^
// No need to fully qualify the name thanks to the using declaration
};
【讨论】:
string 类(属于标头)在 std 命名空间内定义。在对象声明中 string 之前缺少 using std::string; 或 std::。
如果仍然无法解决,请查看this answer。
【讨论】:
尝试创建一个新的控制台项目并将其保留在下面这个简单的代码中。如果这不起作用,那么您可能没有为 c++ 正确设置 eclipse。 eclipse c++环境的默认下载在这里http://www.eclipse.org/cdt/
#include "stdafx.h"//optional depending if you have precompiled headers in VC++ project
#include <string>
using std::string;
class Medicine
{
string name;
};
// or use this alternative main if one below doesn't work
//int main(int argc, _TCHAR* argv[])
int _tmain(int argc, _TCHAR* argv[])
{
Medicine test;
return 0;
}
【讨论】: