【问题标题】:"string" type could not be resolved Eclipse C++ [duplicate]“字符串”类型无法解析 Eclipse C++ [重复]
【发布时间】:2013-03-31 23:29:35
【问题描述】:

我正在尝试这样做:

#include <string>

class Medicine{

    string name;


};

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

【问题讨论】:

  • @David 它不工作。

标签: c++ string


【解决方案1】:

您应该使用它所属的命名空间 (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
};

【讨论】:

  • @CucerzanRares:你能提供更多关于什么不起作用的信息吗?你从编译器得到什么信息?
  • @CucerzanRares,当你说“它不工作”并且不解释问题是什么时,程序员就会死在世界的某个地方。请小心。
  • @CucerzanRares:您的程序是否仅包含这 4 行代码?
  • 这只是Eclipse的编辑抱怨吗?也就是说,您的代码是否编译
  • @CucerzanRares:请尝试编译那个简单的 4 行程序。 里面没有别的东西。如果它没有编译,那么项目设置或某些全局 Eclipse 设置可能有问题。如果简单的最小 4 行程序可以编译,那么这意味着问题出在您拥有的其他东西上
【解决方案2】:

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

如果仍然无法解决,请查看this answer

【讨论】:

  • 这些都不起作用
  • @CucerzanRares 他们有。你错过了什么。
  • @H2CO3 不,它不适合我。我在 Eclipse 中为 C++ 工作
  • @CucerzanRares,观看编辑。
【解决方案3】:

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

【讨论】:

  • 它工作正常。但是当我在头文件中时,它无法识别字符串。
  • 如果你已经 #included 并使用了“使用 std::string;”,我不明白为什么这不起作用在标题中。我认为您需要发布更多代码,以便我们可以看到更多您在做什么。专门发布 eclipse 抱怨的头文件中的代码,与您的项目中出现的完全相同。
猜你喜欢
  • 2021-04-24
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多