简体   繁体   中英

No operator== match for std::string?

I got a strange problem with my program. So in the header I got something like this:

#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];
}

in 1 part of my function in the Set1.cpp file I got something like this :

if (setMember[i] == "foo") {exist = true;}

In this case, I got an error message that says "no operator found which takes a left-hand operand of type 'ItemType' ". However, if I change the std::string in the typedef into int or unsigned long, and change "foo" to some random number, the code works perfectly. Any suggestion? thx

You are missing the header file <string> , which means that you don't have all of the global operator == definitions visible in your program. This is likely the case of your problem.

To fix this, try adding in this line:

#include <string>

Hope this helps!

You need to include string header file to bring relative types and operator to scope.

#include <string>

Note: It's bad coding practice to pull in everything from std namespace in header file.

//using namespace std;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM