0

I'm trying to simply add an object to another object within a class, for example add player to factory.

For my factory.h

class Factory
{
public:

    Factory(void);
    ~Factory(void);
    void addMaze(Maze maze);
    void addPlayer(Player player);
    std::string getSessionTime();
    std::string setSessionTime(std::string time);

    private:

    int m_ID;
    Player m_player;
    Maze m_maze ;
    std::string m_SessionTime;
    std::string m_filePath [50];


    };

and then in my Factory class I have:

void Factory::addPlayer(Player player)
{
    m_player.add(player); //This is what I feel like I want to do
}

So, I'm trying to add a Player to my list of Players, but this won't do it? Am I missing something really obvious? If anyone could help or point me in the right direction I would really appreciate it.

3
  • consider using std::vector<Player> as a member variable of the class Factory. Commented Feb 8, 2013 at 19:54
  • It depends on what you are trying to achieve, is your factory supposed to have a list of players? Commented Feb 8, 2013 at 19:55
  • Fyi you're passing by value, so (assuming no move semantics) the player instance in the Factory won't be the same as the one which addPlayer was called with. It's a copy. Commented Feb 8, 2013 at 20:02

1 Answer 1

4

Player m_player; declares a data member of type Player, not a list of players. If you want a list, have a member std::list<Player> or the more common std::vector<Player>.

Your function could look like

void Factory::addPlayer(const Player& player)
{
    m_players.push_back(player);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.