1

I have a header file where I am declaring a bool operator as a public member of the function RewardCard

        bool operator ==(const RewardCard& card1, const RewardCard& card2);

I have moved all implementation details of the class I am defining into a separate file rewards.cpp which includes the headerfile

I have defined the bool operator as follows:

bool RewardCard::operator ==(const RewardCard& card1, const RewardCard& card2)
{
   return(card1.name == card2.name && card1.id == card2.id && card1.store == card2.store)
}

When compiling the program I am getting an error that says this function must take exactly one argument.

I am avoiding a friend function for overloading as I know these can be achieved using a member function.

What am I doing incorrectly?

Metamorphosis
  • 95
  • 1
  • 1
  • 10

1 Answers1

3

When compiling the program I am getting an error that says this function must take exactly one argument. [...] What am I doing incorrectly?

Your compiler is telling you exactly what you're doing wrong. You are writing operator== as a member function taking the wrong number of arguments. It must take exactly one.

You are comparing the implicit this object to the other object, so your code should be:

bool RewardCard::operator ==(const RewardCard& rhs)
{
   return name == rhs.name && id == rhs.id && store == rhs.store;
}
Barry
  • 286,269
  • 29
  • 621
  • 977