Search code examples
c++eventsinheritancecallbackparent

c++ get instance of parent from child


Hey am creating a game as a school project in c++, and trying to get an instance of the parent class from the child class. I have created a class CollisionHandler witch works as a callback or event listener sort of. This worked until recently but after some changes in my code I can't get it to work again.... What is the proper way to do this ?

class CollisionHandler{
public:
    explicit CollisionHandler(){};
    ~CollisionHandler(){};
    virtual Collision checkWorldCollision(sf::FloatRect playerPos);
    virtual Collision checkPlayerCollision(sf::FloatRect attackPos);
};

Class implementing CollisionHandler:

class Game : public CollisionHandler{
public:

    Game();
    ~Game();

    enum GameType{
        TYPE_LOCAL,
        TYPE_LAN
    };

    enum GameState{
        PLAYING,
        PAUSED,
        ENDED
    };

    void initialize(int width, int height, Map map, GameType type);
    void addPlayer(PlayerInfo& playerInfo, PlayerControls playerControls);
    RunState start(sf::RenderWindow& window);
    RunState resume(sf::RenderWindow& window);
    void pause();
    void exit();

    Collision checkWorldCollision(sf::FloatRect playerPos);
    Collision checkPlayerCollision(sf::FloatRect attackPos);

private:
    RunState run(sf::RenderWindow& window);
    GameState _currState;
    World _world;
    GameType _gameType;
    std::vector<Player> _players;
    int playerAmount;
};

Then am initializing a class Player that uses Game as CallbackHandler:

player.initialize(info, &game);

everything worked fine until recently, but nothing should have affected this part of the code. Getting this error:

error: no matching function for call to ‘Player::initialize(PlayerInfo&, Game*)’
 player.initialize(info, &game);

Also getting this error:

error: ‘CollisionHandler’ has not been declared
 void setCollisionHandler(CollisionHandler* collisionHandler);

Sorry for all the back and forth, but i have narrowed it down to be a declaration error even though everything is included:


Solution

  • Declaration errors, don't know what happened but found something here

    and created a new file and instance of CollisionHandler, everything is working again. Thanks