-3

我正在尝试这样做:

#include <string>

class Medicine{

    string name;


};

但它根本不起作用。我尝试右键单击项目-> 索引-> 搜索未解决的包含,它说:项目中的未解决包含(0 个匹配项)。它也不适用于 std::string 。我应该怎么办?

4

3 回答 3

5

您应该完全符合string它所属的命名空间 ( std):

#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
};
于 2013-03-31T23:30:58.547 回答
1

(标头的string)类在 std 命名空间内定义。您在对象声明中缺少using std::string;std::之前。string

如果您仍然无法修复它,请查看此答案

于 2013-03-31T23:31:03.107 回答
0

尝试创建一个新的控制台项目并将其保留在下面的这个简单代码中。如果这不起作用,那么您可能没有为 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;
}
于 2013-03-31T23:48:38.390 回答