Unable to access base class method in derived class
Unable to access base class method in derived class
I have a base class as follows :
class
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
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
Horse::getSprite
Why is getSprite unavailable in Horse?
getSprite
GamePieceBase is as follows :
GamePieceBase
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
error
GameBoard
@Yksisarvinen yes
– Noah Rose Ledesma
Jul 1 at 21:06
Unable to reproduce the error
– user3366592
Jul 1 at 21:33
What's your compiler?
– user3366592
Jul 1 at 21:52
@user3366592 MSVC++ 14.12
– Noah Rose Ledesma
Jul 1 at 22:12
2 Answers
2
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
virtual
override
final
See op edit....
– Noah Rose Ledesma
Jul 1 at 21:51
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
GameBoardis a namespace?– Yksisarvinen
Jul 1 at 21:04