I'm trying to make a blackjack program where I have a player class and dealer class, and I want the dealer to add a card to the player class's hand. The dealer class should have access to it because it's just protected, but when I use a function that's supposed to add a card, the player class vector remains empty. There's no errors or anything.
1 2 3 4 5 6 7 8 9 10 11
class Player {
protected:
vector<string> playersHand;
};
class Dealer : public Player {
public:
void dealToPlayer() {
playersHand.push_back(draw());
}
};
It looks like you are trying to push a function where a string is supposed to be. Or does the draw function return a string? Also it is protected so you would need a function to view the items in it.
The draw function returns a string, and I'm using a function to display the cards. I have another function that does the same thing but goes to the dealer class's vector and it works.
Edit: Ok figured something out...I can display the player's cards with a function inside the dealer class but I can't make a player object and display them with a function inside the player class after doing the dealToPlayer function. I don't know if that made sense. It becomes like a different vector...I get it now.
When you call dealToPlayer(), the dealer draws a card which is then pushed onto his playersHand vector.
Something like this perhaps:
1 2 3 4 5 6 7 8 9 10 11
class Dealer : public Player {
public :
Dealer() {}//whatever
void deal(Player* player);
//etc
};
void Dealer::deal(Player* player) {
player->hand.push_back(draw());
//assuming draw() is not a member function.
}