我的基本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;
}
还有派生的class
:
class Horse : public GameBoard::GamePiece< GameBoard::GamePieceBase >
{
public:
virtual sf::Sprite* getSprite() override
{
// Do stuff
}
};
但是我遇到Horse::getSprite
减速时出现的错误
用override声明的成员函数不会覆盖基类成员
为什么getSprite
在Horse中不可用?
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 ){}
};
编辑:
输出日志表明存在内部编译器error
,但未提供许多详细信息。我正在对此进行调查
答案 0 :(得分:1)
这对我有用:
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;
}
};
但是,最好写:
int getSprite() override
写显式virtual
,override
或final
是自记录文档,使编译器能够捕获基类和派生类之间类型和/或名称的不匹配。但是,写这三者中的一个以上既多余又是潜在的错误源。 C++ CoreGuidelines C.128
答案 1 :(得分:0)
问题出在线
typedef aGamePieceBase gamePieceBase
在此忘记分号会使随后的方法减速无效,并在MSCV编译器输出中引起奇怪的错误。