没有operator ==匹配std :: string?

use*_*385 0 c++ string visual-c++

我的程序遇到了一个奇怪的问题.所以在标题中我得到了这样的东西:

#ifndef SET1_INCLUDED
#define SET1_INCLUDED

#include <iostream>
using namespace std;

typedef std::string ItemType;
class Set1{
  public:
  ------some public constructor and method in here-------
  private:
  ItemType setMember[100];
}
Run Code Online (Sandbox Code Playgroud)

在Set1.cpp文件中我的函数的一部分我有这样的东西:

if (setMember[i] == "foo") {exist = true;}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我收到一条错误消息,指出"没有找到哪个运算符,它采用'ItemType'类型的左手操作数".但是,如果我将typedef中的std :: string更改为int或unsigned long,并将"foo"更改为某个随机数,则代码可以正常工作.有什么建议吗?谢谢

tem*_*def 8

您缺少头文件<string>,这意味着operator ==您的程序中没有可见的所有全局定义.这可能是你的问题.

要解决此问题,请尝试在此行中添加:

#include <string>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!