pocoa
pocoa

Reputation: 4337

Calling a method of a constant object parameter

Here is my code that fails:

bool Table::win(const Card &card) {
   for (int i = 0; i < cards.size(); i++)
      if (card.getRank() == cards[i].getRank()) return true;

   return false;
}

Error message is: passing 'const Card' as 'this' argument of 'int Card::getRank()' discards qualifiers.

When I get a copy of the card and change the code to this it works:

bool Table::win(const Card &card) {
   Card copyCard = card;

   for (int i = 0; i < cards.size(); i++)
      if (copyCard.getRank() == cards[i].getRank()) return true;

   return false;
}

Is there any other way to do this?

Upvotes: 5

Views: 9133

Answers (1)

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76788

Is getRank a const-method? It should be declared like this":

int getRank( ) const;

Assuming the return type is int.

Upvotes: 18

Related Questions