Springfox
Springfox

Reputation: 1419

Adding an object to another object C++

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.

Upvotes: 0

Views: 172

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258618

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);
}

Upvotes: 4

Related Questions