Noah Rose Ledesma
Noah Rose Ledesma

Reputation: 319

Unable to access base class method in derived class

I have a base class as follows :

template<typename aGamePieceBase> class GamePiece : public aGamePieceBase
{
public:
    // Each GamePiece contains a definition of the base class from which it was derived
    typedef aGamePieceBase gamePieceBase

    // Returns this piece's sprite
    virtual sf::Sprite* getSprite()
    {
        return m_pSprite;
    }
protected:
    sf::Sprite* m_pSprite;
}

And a derived class :

class Horse : public GameBoard::GamePiece< GameBoard::GamePieceBase >
{
public:
    virtual sf::Sprite* getSprite() override
    {
        // Do stuff
    }
};

However I am presented with this error on the deceleration of Horse::getSprite Member function declared with override does not override a base class member

Why is getSprite unavailable in Horse?

GamePieceBase is as follows :

class GamePieceBase : public GameEventHandler
{
public:
    // Called when the GamePiece has been added to the GameBoard 
    template<typename aTileBase> virtual void onPieceCreate( Tile<aTileBase> *startingLocation ){}
    // Called when the GamePiece has been removed from the GameBoard
    virtual void onPieceRemove(){}
    // Called when the user has selected this GamePiece
    virtual void onPieceSelect(){}
    // Called when the user has de-selected this GamePiece
    virtual void onPieceDeselect(){}
    // Called when this GamePiece has moved on the board
    template<typename originTileBase, typename destinationTileBase> virtual void onPieceMove( Tile<originTileBase>* originTileBase, Tile<destinationTileBase>* pDestinationTile ){}
};

EDIT : Output log is indicating there is an internal compiler error but does not give many details. I am investigating this now

Upvotes: 2

Views: 541

Answers (2)

Noah Rose Ledesma
Noah Rose Ledesma

Reputation: 319

The issue was with the line

typedef aGamePieceBase gamePieceBase

Forgetting a semicolon here invalidated the subsequent method deceleration and caused strange errors in the MSCV compiler output.

Upvotes: 0

Amit G.
Amit G.

Reputation: 2674

This works for me:

class GamePieceBase
{
public:
    // Called when the GamePiece has been added to the GameBoard 
};


template<typename aGamePieceBase> class GamePiece : public aGamePieceBase
{
public:
    // Each GamePiece contains a definition of the base class from which it was derived
//  typedef aGamePieceBase gamePieceBase

    // Returns this piece's sprite
    virtual int getSprite()
    {
        return 0;
    }
protected:

};


class Horse : public GamePiece< GamePieceBase >
{
public:
    virtual int getSprite() override
    {
        return 0;
    }
};

However, it is better to write:

int getSprite() override

Writing explicit virtual, override, or final is self-documenting and enables the compiler to catch mismatch of types and/or names between base and derived classes. However, writing more than one of these three is both redundant and a potential source of errors. C++ CoreGuidelines C.128

Upvotes: 1

Related Questions