Dane Wind
Dane Wind

Reputation: 21

Class constructor problems

I am currently writing a C++ single player, command line, blackjack game. The main problem I am having is that whenever my Player class default constructor,

    Player(const std::string& name = "");

is constructed, it seems to be defaulting even though there is input coming in from the command line to assign a name.

My Dealer class default constructor seems to work just fine because is isn't initialized to an empty string like my Player class.

    Dealer(const std::string& name = "Dealer");

It almost seems as though my code is completely forgetting the data from my input for the players' name.

I tried to narrow the code as much possible to find the problem and I have yet to find a solution. There are no compiler errors, but my player names just aren't showing up.

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
#include <algorithm>

class Player
{
public:
    Player(const std::string& name = "");
    virtual ~Player();
    friend std::ostream& operator <<(std::ostream& str, const Player& aPlayer);
protected:
    const std::string m_Name;
};

Player::Player(const std::string& m_name) : m_Name(m_name) {}
Player::~Player() {}

class Dealer : public Player
{
public:
    Dealer(const std::string& name = "Dealer");
    virtual ~Dealer();
};

Dealer::Dealer(const std::string& name) : Player(name) {}
Dealer::~Dealer() {}

class Game 
{
public:
    Game(const std::string& name);
    ~Game();
    void Play();
protected:
    Player newPlayer;
};

Game::Game(const std::string& name)
{
    srand(time(0));
    // I feel like something else needs to go here to initialize a player or something. Not sure what.

}
Game::~Game() {}

void Game::Play()
{
    std::cout << newPlayer << std::endl;
    // Possible this could be messing it up?
}

std::ostream& operator <<(std::ostream& str, const Player& aPlayer);

int main()
{
    std::cout << "Enter your name: ";
    std::string name;
    std::cin >> name;
    Game Game(name);
    Game.Play();
    return 0;
}

std::ostream& operator <<(std::ostream& str, const Player& aPlayer)
{
    str << aPlayer.m_Name << ":\t";

    return str;
}

And when I run without debugging, this is my result:

Enter your name: Dane
:
Dealer:
Press any key to continue . . .

I'm expecting to get a name (Dane in this case) before that first colon and it doesn't want to appear. I can't figure out the problem in my code and was wondering if anyone could give me some insight on errors in my coding.

Upvotes: 1

Views: 120

Answers (3)

Schwinschwiga
Schwinschwiga

Reputation: 1

Define Your Game class as follows, and do not for get to clear the pointer to Player in your destructor

class Game 
{
public:
    Game(const std::string& name);
    ~Game();
    void Play();
protected:
    Player *newPlayer;
};

Game::Game(const std::string& name)
{
    srand(time(0));
    // I feel like something else needs to go here to initialize a player or something. Not sure what.
    this->newPlayer = new Player( name );
}


void Game::Play()
{
    std::cout << (*newPlayer) << std::endl;
    // Possible this could be messing it up?
}

Upvotes: -1

Tio Pepe
Tio Pepe

Reputation: 3099

You forgot to pass name to your player:

Game::Game(const std::string& name) :
    newPlayer(name)
{
    srand(time(0));
}

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409432

In the Game constructor you should initialize the Player object newPlayer, passing on the argument to it (how else would the newPlayer object be initialized with the name?).

You do this with a constructor member initializer list:

Game::Game(const std::string& name)
    : newPlayer(name)  // This is the member initializer list
{
    ...
}

See e.g. this constructors and member initializer lists reference for more information.

Upvotes: 1

Related Questions