otch92
otch92

Reputation: 1

operator overloading error in C++ when trying to compare strings

I am trying to overload an operator << in a class I created which compares two strings no matter the casing. Here is my bool operator

friend bool operator <<(const string& member, const string& player) {
  return toLowerCase(member) == toLowerCase(player);
}

The program returns an error at this point in my code

current->name << member

Now current is a pointer to a node which contains a string name and member is a string that is passed by reference when the user is prompted. The error I get is

invalid operands to binary expression('string'(aka'basic_string<char>') and 'string')

Upvotes: 0

Views: 119

Answers (2)

masoud
masoud

Reputation: 56479

Move that declaration out of your class and remove friend keyword:

bool operator <<(const string& member, const string& player)
{
     return toLowerCase(member) == toLowerCase(player);
}

Live code.

Upvotes: 0

Tristan Brindle
Tristan Brindle

Reputation: 16824

The error message suggests it's not picking up your overload, but trying to use the built-in bitshift operator on strings (and failing, of course). I'm not sure why, given the information you've supplied.

Anyway, a much better idea would be to use a non-member, non-operator compare() function -- using operator<< to compare strings is just going to confuse the hell out of anybody looking at your code (even you, in a couple of years time).

Upvotes: 1

Related Questions