8

我在 MSVC++ 2008 中遇到问题,VS2008 抛出此编译错误:

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;
}

是一个例子。

4

3 回答 3

17

在 PlayerSpriteKasua.h 中,您需要重新声明要覆盖/实现的任何方法(没有“=0”表示这些方法不再是抽象的)。所以你需要这样写:

class PlayerSpriteKasua : public PlayerSpriteBase
{
public:
    virtual void think();
    virtual void render(long long ScreenX, long long ScreenY);
    virtual int getHealth();
};

...或者您是否忽略了这一点以使您的帖子更短?

于 2009-10-23T21:11:46.573 回答
2

您需要在类定义中为 PlayerSpriteKasua::render() 提供声明。否则,包括您的 PlayerSpriteKasua.h 在内的其他翻译单元将无法判断您提供了定义,并被迫得出无法实例化 PlayerSpriteKasua 的结论。

于 2009-10-23T21:11:03.220 回答
2

您需要在 PlayerSpriteKasua.h 中的 PlayerSpriteKasua 声明中重新声明要在 PlayerSpriteKasua 中实现的 SpriteBase 成员。

于 2009-10-23T21:11:26.637 回答