4

I have defined a class like this

using namespace std;
class foo {
public:
  typedef std::pair< int, int > index;
  bool operator == ( const index &l, const index &r )
  {
    return (l.first == r.first && l.second == r.second);
  }
  void bar()
  {
    index i1;
    i1.first = 10;
    i1.second = 20;
    index i2;
    i2.first = 10;
    i2.second = 200;
    if (i1 == i2)
       cout << "equal\n";
  }
};

However I get this error in windows

error C2804: binary 'operator ==' has too many parameters

and this error in linux

operator==(const  std::pair<int, int>&, const std::pair<int, int>&)’ must take exactly one argument

I found this topic overloading operator== complaining of 'must take exactly one argument' and seems to be a problem with static and non-static functions in a class. However I don't know how to apply this

For example, this is not correct

  bool operator == ( const index &r )
  {
    return (this->first == r.first && this->second == r.second);
  }

How can I fix that?

Community
  • 1
  • 1
mahmood
  • 23,197
  • 49
  • 147
  • 242

4 Answers4

5

The operator== can be implemented in two ways:

  • As member function: in this case, the function takes one argument and is invoked on the left operand which is passed as this pointer implicitly to the function.
  • As non-member function, in which case, the function takes two arguments, left and right operands.

Since you're implementing operator== for std::pair, you cannot implement it as member function (of std::pair). The option you're left with is non-member function.

So implement it outside the class as:

bool operator==(std::pair<int,int> const & l, std::pair<int,int> const & r)
{
    return (l.first == r.first && l.second == r.second);
}

But then you don't really need to implement it yourself unless you want to implement it differently. The Standard Library has already provided a generic version of operator== for std::pair which lexicographically compares the values in the pair, like I did above, i.e compare first with first and second with second. If you need to compare them differently, only then provide your own specific definition (non-template version).

The above mentioned points are worth noting as to how to implement operator== when you need it for your defined types.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 2
    Please don't do this though. `std::pair` already has a generic `operator==` overload that does the sensible thing (compares first against first, and second against second). Just create your own struct that holds two ints, not as a typedef of `std::pair`. – Benjamin Lindley Jan 20 '13 at 09:48
  • @BenjaminLindley: Yes. I wrote that. – Nawaz Jan 20 '13 at 09:49
  • @Nawaz: My comment was for the OP though. Notice that the standard library version will *not* work for his case, since he compares lhs.first against rhs.second, and completely ignores rhs.first. That's why I recommended creating his own struct. Unless that was a typo. – Benjamin Lindley Jan 20 '13 at 09:51
  • 1
    No that was a typo. first with first, second with second. Thanks for the comment – mahmood Jan 20 '13 at 09:54
  • @BenjaminLindley: I notice that now, and I think that is a typo. – Nawaz Jan 20 '13 at 09:55
3

You need to move operator== out of class foo:

bool operator == ( const foo::index &l, const foo::index &r )
{
  return (l.first == r.second && l.second == r.second);
}

class foo {
public:
   typedef std::pair< int, int > index;
  void bar()
  {
    index i1;
    i1.first = 10;
    i1.second = 20;
    index i2;
    i2.first = 10;
    i2.second = 200;
    if (i1 == i2)
       cout << "equal\n";
  }
};

Also note, std::pair has overload operator== already, see: link, you might reconsider if necessary to write your own again.

billz
  • 44,644
  • 9
  • 83
  • 100
2

If you overload the == operator inside a class, it should only take a single parameter so that comparison can be done between the current object and the argument.

sgarizvi
  • 16,623
  • 9
  • 64
  • 98
2

You can move that operator out of the class, that way you can take 2 operands. Indeed there is no point of keeping it within the class at this point, since you are only comparing member variables and not the class itself.

Indeed I wont be surprised if pair already defines the operator you write.

Edit : Yup It looks like pair already implements this

Two pair objects are compared equal if the first elements in both objects compare equal to each other and both second elements also compare equal to each other - they all have to match.

p.s. I think you meant

return (l.first == r.first && l.second == r.second);
                    ^^^^^^
Karthik T
  • 31,456
  • 5
  • 68
  • 87