[英]C++ - “Member function not declared” in derived class
我在VS2008抛出此编译错误的MSVC ++ 2008中遇到问题:
error C2509: 'render' : member function not declared in 'PlayerSpriteKasua'
现在,令我困惑的是render()是定义的,但是是在继承的类中。
类的定义如下:
SpriteBase -Inherited By-> PlayerSpriteBase -Inherited By-> PlayerSpriteKasua
因此,以下是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-
//======================================================
};
PlayerSpriteBase.h是这样的:
class PlayerSpriteBase : public SpriteBase
{
public:
virtual void pose() = 0;
virtual void knockback(bool Direction) = 0;
virtual int getHealth() = 0;
};
最后,PlayerSpriteKasua.h是这样的:
class PlayerSpriteKasua : public PlayerSpriteBase
{
public:
};
我知道现在还没有成员,但这仅仅是因为我还没有添加他们。 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;
}
当我键入内容时,例如, void PlayerSpriteKasua::
,Intellisense会弹出,列出PlayerSpriteBase和SpriteBase的所有成员都很好,但是在编译时会失败,如上所述。
我收到此错误有什么特殊原因吗?
PlayerSpriteBase.cpp是空的,到目前为止还没有任何内容。
SpriteBase.cpp具有许多SpriteBase的函数定义,并使用与PlayerSpriteKasua.cpp相同的格式:
void SpriteBase::died() {
return;
}
是一个例子。
在PlayerSpriteKasua.h中,您需要重新声明将要覆盖/实现的任何方法(不用“ = 0”表示这些方法不再是抽象的)。 因此,您需要像下面这样写:
class PlayerSpriteKasua : public PlayerSpriteBase
{
public:
virtual void think();
virtual void render(long long ScreenX, long long ScreenY);
virtual int getHealth();
};
...或者您是否忽略了这一点以缩短您的帖子?
您需要在类定义中提供PlayerSpriteKasua :: render()的声明。 否则,其他翻译单元(包括PlayerSpriteKasua.h)将无法告知您已提供了定义,并且将被迫得出无法实例化PlayerSpriteKasua的结论。
您需要在PlayerSpriteKasua.h中的PlayerSpriteKasua声明中重新声明要在PlayerSpriteKasua中实现的SpriteBase成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.