Suk*_*asa 7 c++ virtual inheritance class abstract
我在MSVC++ 2008中遇到问题,其中VS2008抛出了这个编译错误:
error C2509: 'render' : member function not declared in 'PlayerSpriteKasua'
Run Code Online (Sandbox Code Playgroud)
现在,令我困惑的是,render()已定义,但是在继承的类中.
类定义的工作方式如下:
SpriteBase -Inherited By-> PlayerSpriteBase -Inherited By-> PlayerSpriteKasua
Run Code Online (Sandbox Code Playgroud)
因此,SpriteBase.h的精简版本如下:
class SpriteBase {
public:
//Variables=============================================
-snip-
//Primary Functions=====================================
virtual void think()=0; //Called every frame to allow the sprite to process events and react to the player.
virtual void render(long long ScreenX, long long ScreenY)=0; //Called every frame to render the sprite.
//Various overridable and not service/event functions===
virtual void died(); //Called when the sprite is killed either externally or via SpriteBase::kill().
-snip-
//======================================================
};
Run Code Online (Sandbox Code Playgroud)
PlayerSpriteBase.h是这样的:
class PlayerSpriteBase : public SpriteBase
{
public:
virtual void pose() = 0;
virtual void knockback(bool Direction) = 0;
virtual int getHealth() = 0;
};
Run Code Online (Sandbox Code Playgroud)
最后,PlayerSpriteKasua.h是这样的:
class PlayerSpriteKasua : public PlayerSpriteBase
{
public:
};
Run Code Online (Sandbox Code Playgroud)
我知道它还没有成员,但那仅仅是因为我没有添加它们.PlayerSpriteBase也是如此; 还有其他的东西要进去.
PlayerSpriteKasua.cpp中的代码是这样的:
#include "../../../MegaJul.h" //Include all the files needed in one go
void PlayerSpriteKasua::render(long long ScreenX, long long ScreenY) {
return;
}
void PlayerSpriteKasua::think() {
return;
}
int PlayerSpriteKasua::getHealth() {
return this->Health;
}
Run Code Online (Sandbox Code Playgroud)
比方说,当我键入时,void PlayerSpriteKasua::Intellisense会弹出列出PlayerSpriteBase和SpriteBase的所有成员就好了,但是在编译时就像我上面说的那样失败了.
有什么特别的原因我收到这个错误吗?
PlayerSpriteBase.cpp为空,但尚未包含任何内容.
SpriteBase.cpp有很多SpriteBase的函数定义,并使用与PlayerSpriteKasua.cpp相同的格式:
void SpriteBase::died() {
return;
}
Run Code Online (Sandbox Code Playgroud)
就是一个例子.
Lud*_*ant 16
在PlayerSpriteKasua.h中,你需要重新声明你要覆盖/实现的任何方法(没有"= 0"表示那些方法不再是抽象的).所以你需要写如下:
class PlayerSpriteKasua : public PlayerSpriteBase
{
public:
virtual void think();
virtual void render(long long ScreenX, long long ScreenY);
virtual int getHealth();
};
Run Code Online (Sandbox Code Playgroud)
...或者你是否省略了这一点以保持你的帖子更短?