Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9103305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:36:17+00:00 2026-06-17T01:36:17+00:00

I’m currently working on a Poker game for C++, and I’m having trouble properly

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:36:18+00:00Added an answer on June 17, 2026 at 1:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.