Struggling to cout vector when using classes

I'm currently trying to make a blackjack game, and I try to cout different values to check if I'm on the right path. I'm new to classes, and I don't really understand how to use them correctly yet.
But my question is how I can cout vector elements in "playBlackjack()"
I currently get a red line under the "<<" in "playBlackjack()" with the "no operator "<<" matches these operands. Operand types are: std::ostream << Card" error.


1
2
3
4
5
void Blackjack::dealerDraw() {
	CardDeck cd;
	Card dealerCard = cd.drawCard();
	dealerHand.push_back(dealerCard);
}


1
2
3
4
5
void playBlackjack() {
	Blackjack bj;
	bj.dealerDraw();
        cout << bj.dealerHand[0];
}


Classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Card {
private:
	Rank r;
	Suit s;
	bool valid;
	
public:
	Card(Suit suit, Rank rank);
	Card();
	Suit getSuit();
	Rank getRank();
	bool isValid();
	string toString();
	string toStringShort();
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CardDeck {
private:
	vector<Card> cards;
	int currentCardIndex;
public:
	CardDeck();
	void fillDeck();
	void swap(int en, int to);
	void print();
	void printShort();
	void shuffle();
	const Card& drawCard(); 

};

1
2
3
4
5
6
7
8
9
class Blackjack {
private:

public:
	vector<Card> dealerHand;
	vector<Card> playerHand;
	void dealerDraw();
	void playerDraw();
};
You have no overloaded operator << for the class Card. You need one since the vector element you're trying to display is of Card type, or you need a display function within that card class to display its variables

also in your card class what are Rank and Suit?
First of all, thanks for answering!
Ah okey, do you have an example on how I can overload the operator << (don't know if I use the terminology correctly, so sorry for that) or how to set up a display function?
I don't understand how to implement the tips:/

Oh yeah sorry, forgot to include that..
1
2
3
4
5
6
7
enum class Suit {
	clubs,diamonds,hearts,spades
};

enum class Rank{
	two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace
};
Both your data members are private. What are you trying to display?

When your data members are private, you need a method to help you with displaying.
1
2
3
void printCard() {
   std::cout << "Rank: " << r << "\nSuit: " << s;
} // Should be inside the class, under public 


If your data members were public then you could overload the << operator to accept 'Card' type.
1
2
3
4
ostream& operator<<(ostream& os, const Card& card) {
    os << "Rank: " << card.r << "\nSuit: " << card.s;
    return os;
}


Cheers.

edit: Btw if your data members were public then you could also do this
1
2
std::cout << bj.dealerHand[0].r; // Rank of first member
std::cout << bj.dealerHand[0].s; // Suit of first member 


To display all elements of the vector:
1
2
3
4
for(size_t i = 0, size = bj.dealerHand.size(); i < size; i++) {
   std::cout << "Card No." << i << "\nRank: " << bj.dealerHand[i].r;
   std::cout << "\nSuit: " << bj.dealerHand[i].s << "\n\n";
}


Last edited on
That cleared things up a bit.
Thanks!
Topic archived. No new replies allowed.