2

When I try to add bool operator==(complx a, complx b) it says

  |Code      |Description
  |EO344     too many parameters for this operator function

I have tried using just one parameter, but I can't do it.

Here is the actual function:

    //boolean operators                          "|Code  |Description
    bool operator==(complx a, complx b) //error: " EO344 too many parameters for this operator function"
    {
        /*error code*/return a.real() == b.real() && a.imag() == b.imag();
    }

It says there are too many parameters, but my book says there's not. Can you help me?

bolov
  • 72,283
  • 15
  • 145
  • 224
Dukk
  • 106
  • 7
  • Possible duplicate of [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – JaMiT Jul 08 '19 at 05:24
  • In particular, see the first footnote in the "Comparison operators" section of [this answer](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading/4421719#4421719). (I know, it's buried, but it is there, and the linked question is a good reference.) – JaMiT Jul 08 '19 at 05:25
  • @JaMiT I can't even manage to find it. Where really is it? – Dukk Nov 17 '19 at 18:37
  • The sections in that answer are "Assignment Operator", "Bitshift Operators (used for Stream I/O)", "Function call operator", **"Comparison operators"**, "Arithmetic Operators", "Array Subscripting", and "Operators for Pointer-like Types". So it's the fourth of the seven sections, roughly at the middle of the question. – JaMiT Nov 17 '19 at 23:03

1 Answers1

5

As a member function, operator==() must take one parameter, which is the right-hand side operand. The left-hand side operand is the this object.

It only takes two parameters when implemented as as a free, non-member function.

Note that when implementing binary operators as member functions you can run into ordering problems. See this answer for details.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96