11

I am trying to overload the operator==, but the compiler is throwing the following error:

‘bool Rationalnumber::operator==(Rationalnumber, Rationalnumber)’ must take exactly one argument

My short piece of code is as follows:

bool Rationalnumber::operator==(Rationalnumber l, Rationalnumber r) {
  return l.numerator() * r.denominator() == l.denominator() * r.numerator();
}

Declaration:

bool operator==( Rationalnumber l, Rationalnumber r );

Does anyone have any ideas why it's throwing the error?

chlong
  • 445
  • 2
  • 4
  • 8
  • 2
    This might help: http://stackoverflow.com/questions/4421706/operator-overloading. Since yours is a member, though, it already has the left side coming in implicitly through the hidden `this` argument. – chris Jun 27 '12 at 14:56
  • 2
    You must define the member funtion with one argument or the file scope function with two arguments. – harper Jun 27 '12 at 14:56
  • Is it a member function or a free standing function? – Anon Mail Jun 27 '12 at 14:57
  • It's telling you, the member function must have only one argument. Makes sense, since you'll be comparing `this` and the argument. If it were a free function (as it should be), then it'll take two values to compare, so have two arguments. – GManNickG Jun 27 '12 at 14:57
  • is it a free function or a member function? Also, you should pass const Rationalnumber& instead of an object. – Alessandro Teruzzi Jun 27 '12 at 14:57
  • 1
    I think the `RationalNumber::operator==` part pretty clearly explains which one it is. – chris Jun 27 '12 at 14:59
  • I see thanks guys. It is a member function, I will remove the class scope and try it now. – chlong Jun 27 '12 at 15:01
  • @chlong or leave it as a member function with a single parameter. In that case, it would make sense to make it `const`. – juanchopanza Jun 27 '12 at 15:05
  • 3
    @juanchopanza: A non-member is a better option if the type supports implicit conversion. A member would allow `num == 0`, but not `0 == num` while a non-member would allow both. – Mike Seymour Jun 27 '12 at 15:16

4 Answers4

21

If operator== is a non static data member, is should take only one parameter, as the comparison will be to the implicit this parameter:

class Foo {
  bool operator==(const Foo& rhs) const { return true;}
};

If you want to use a free operator (i.e. not a member of a class), then you can specify two arguments:

class Bar { };
bool operator==(const Bar& lhs, const Bar& rhs) { return true;}
GManNickG
  • 494,350
  • 52
  • 494
  • 543
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
4

As a member operator overload it should only take one argument, the other being this.

class Foo
{
    int a;

public:
    bool operator==(const Foo & foo);
};

//...

bool Foo::operator==(const Foo & foo)
{
    return a == foo.a;
}
Plexico
  • 131
  • 1
  • 4
2

You should remove your operator== from a RationalNumber to somewhere else. As it is declared inside a class it is considered that 'this' is the first argument. From semantics it is seen that you offer 3 arguments to a compiler.

tim-oleksii
  • 133
  • 9
1
friend bool operator==( Rationalnumber l, Rationalnumber r );

when you declare it as non-member function, it can take two arguments. when you declare it as member function, it can only take one argument.

julienc
  • 19,087
  • 17
  • 82
  • 82
yucun li
  • 11
  • 1