I am studying cpp now, when i implement a card class with the print operator it’s sad"Invalid operands to binary expression (‘std::ostream’ (aka ‘basic_ostream’) and ‘const Suits’)" .Also, I’m using Xcode for IDE. Thanks for the help!
#include <iostream>
enum class Suits : char {clubs='C',diamonds='D',hearts='H',spades='S'};
enum class Faces : int {two=2, three, four, five, six, seven, eight,
nine, ten, jack, queen, king, ace /*14*/};
class Card {
public:
Card(Faces aFace=Faces::ace, Suits aSuit=Suits::clubs){
suit=aSuit;
face=aFace;
}
~Card();
friend std::ostream &operator<<(std::ostream& output, const Card &s){
output << s.suit;
return output;
}
protected:
Suits suit;
Faces face;
};
2
Answers
According to the error message you did not define the
operator <<
for objects of the typeSuits
that is used within the body of theoperator <<
for objects of the typeCard
That is you need also to define the operator
Perhaps you need also to define a similar operator for objects of the type
Faces
.You need to implement
std::ostream &operator<<(std::ostream& output, const Suits &s)
on top of the Card implementation.You can simply implement it inside the Suits class, like you did with the Card class.