2

I'm currently working on a Poker game for C++, and I'm having trouble properly updating a data member for one of my class objects. The game currently uses four classes: Game, Player, Hand, and Card (I'm not focusing on my House class just yet). Game contains the Player objects (number defined by user), Player contains a Hand object, and Hand contains the Card object (two cards for each player). Here's the relevant code I have so far:

The game is immediately ran from main() via a new Game object. After the number of players and player names are assigned to each player object, the player information is displayed (for testing purposes).

#include <iostream>
using namespace std;

Game::Game() : numPlayers(0), players(NULL)
{
    numPlayers = promptNumPlayers();
    players = new Player[numPlayers];

    cout << endl;

    for (int i = 0; i < numPlayers; i++)
    players[i].setName(promptName(i+1));

    play();
}

void Game::play()
{
    cout << endl;

    for (int i = 0; i < numPlayers; i++)
        cout << *(players+i); //displays incorrect hand
}

In this cout test, the player's information is displayed properly (albeit the same cards, but I will fix that later). I suppose this means that my Hand and Card classes are okay for now, but I'll post those if necessary.

Player::Player() : name(""), money(10000), playerHand(NULL)
{
    Hand newHand(NUM_CARDS);
    Hand *tempPtr = &newHand;
    playerHand = tempPtr;

    cout << *playerHand; //displays correct hand
}

ostream& operator<<(ostream &out, const Player &aPlayer)
{
    out << "\n* " << aPlayer.name << ": ";
    out << "$" << aPlayer.money << "  ";
    out << *(aPlayer.playerHand);

    return out;
}

In the play() function, however, the player's hand is blank. I'm not sure if I'm improperly updating playerHand, or if some data is being destroyed when I try to display it in play(). Is Player's constructor incorrect, or do I need to add more code somewhere else? I've tried using a setter for Hand, but it hasn't worked out so far.

If I change the output operator to display playerHand's memory address, the player's constructor will still display the correct hand, and play() will instead display the memory address. Otherwise, I think it works correctly.

I've come across a similar problem in one of my other programs, but I was able to fix it myself. I assume this issue is a bit more complex because I'm using an additional class in this program for holding this data.

Jamal
  • 763
  • 7
  • 22
  • 32
  • 1
    I highly recommend a `std::vector` instead of `new[]`. – chris Jan 05 '13 at 02:55
  • Just for Player, right? I can try to do that; I've only used new here because I have less experience with vectors. – Jamal Jan 05 '13 at 03:52
  • 1
    Well, keep practising vectors. They save you a lot of headaches when you forget to delete something, or when you accidentally use the wrong delete, or when you forget about the rule of three, or when you need to resize it, among many other things. They take the same form as the rest of the standard containers, so once you know one, you can use all of them with relative ease. The other part is that they utilize RAII, which is why you never have to worry about cleaning up after the array itself - the vector cleans it up! – chris Jan 05 '13 at 04:05

1 Answers1

3

You are assigning to playerHand a reference to an object that has automatic allocation on stack.

You should allocate it on heap:

playerHand = new Hand(NUM_CARDS);

Otherwise what happens is that the hand is allocated on stack inside the constructor, when the variable newHand goes out of scope it is released since stack is fred so the pointer playerHand is not valid anymore.

Remember to release its memory through delete playerHand in Player destructor.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • It works! I was trying similar allocation before, but I hadn't known that that syntax works for allocating a new object. I was using that previous syntax because I thought it was okay to use a "temporary" object, while trying to create an overloaded copy constructor. I've also never truly been taught the importance of the stack and heap. I'll learn more about this. Thanks so much! – Jamal Jan 05 '13 at 03:13
  • Take a look here: http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Jack Jan 05 '13 at 03:17
  • In that case, I suppose heap allocation is always best for updating data members. Perhaps if I was better at debugging with Visual, I would've detected this. I've now accepted your answer, and I'll try to get better at using this forum over time. I'm glad to finally have a way to get help on my programs (ALL of them are personal, not homework), and I only wish I'd come here sooner. Thanks again for helping me with my first forum post! I'll try to answer some existing questions if I can. – Jamal Jan 05 '13 at 03:29