Search code examples
c++ooppointersgoogletestgetter-setter

set one class' object into another class' method


I have to write my method according to this GT testCase:

TEST(playerTest, setGameTest) {

    Player p;
    Game g;
    p.setGame(&g);
    EXPECT_EQ(&g, p.getGame());
}

Now Player.h has these:

Game* game;

void setGame(Game* g);
Game getGame();

Player.cpp

void Player::setGame(Game* g) {
    this->game =  g;
}


int Player::getGame() {
    return this->game;
}

but these don't work with the test due to incompatible pointer types. I would appreciate if I could also get some explanation along with the solution.


Solution

  • Your setter and getter do not have the right signatures

    void Player::setGame(Game* g)
    {
        game = g;
    }
    
    Game* Player::getGame() const
    {
        return game;
    }