Home > Mobile >  'operator <<' why is it not working,the print method. c
'operator <<' why is it not working,the print method. c

Time:10-07

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!

enter image description here

#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;
};

CodePudding user response:

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.

CodePudding user response:

According to the error message you did not define the operator << for objects of the type Suits that is used within the body of the operator << for objects of the type Card

output << s.suit;

That is you need also to define the operator

std::ostream & operator <<( std::ostream &, const Suits & );

Perhaps you need also to define a similar operator for objects of the type Faces.

  • Related