C++ - “未声明成员函数”在派生类中

发布于 2024-08-08 23:21:26 字数 2149 浏览 4 评论 0原文

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

是一个示例。

I have a problem in MSVC++ 2008 where VS2008 is throwing this compile error:

error C2509: 'render' : member function not declared in 'PlayerSpriteKasua'

Now, what's confusing me is that render() is defined, but in an inherited class.

The class definition works like this:

SpriteBase -Inherited By-> PlayerSpriteBase -Inherited By-> PlayerSpriteKasua

So, a pared-down version of SpriteBase.h is the following:

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 is this:

class PlayerSpriteBase : public SpriteBase
{
public:
  virtual void pose() = 0;
  virtual void knockback(bool Direction) = 0;
  virtual int getHealth() = 0;
};

And finally, PlayerSpriteKasua.h is this:

class PlayerSpriteKasua : public PlayerSpriteBase
{
public:
};

I know there are no members in it yet, but that's simply because I hadn't gotten to adding them. Same goes for PlayerSpriteBase; there's other stuff left to go in to it.

The code in PlayerSpriteKasua.cpp is this:

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

When I type, say, void PlayerSpriteKasua::, Intellisense pops up listing all the members of PlayerSpriteBase and SpriteBase just fine, but on compile it fails like I said above.

Is there any particular reason I'm getting this error?

PlayerSpriteBase.cpp is empty and has nothing in it as of yet.

SpriteBase.cpp has plenty of function definitions for SpriteBase, and uses the same format as PlayerSpriteKasua.cpp:

void SpriteBase::died() {
  return;
}

is an example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放血 2024-08-15 23:21:26

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

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

...或者你是否省略了它以使你的帖子更短?

In PlayerSpriteKasua.h you need to re-declare whatever methods you're going to override/implement (without the "=0" to say that those methods are not abstract anymore). So you need to write it like follows:

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

...or did you omit that to keep your post shorter?

橪书 2024-08-15 23:21:26

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

You need to provide a declaration for PlayerSpriteKasua::render() in your class definition. Otherwise, other translation units including your PlayerSpriteKasua.h wouldn't be able to tell that you'd provided a definition, and would be forced to conclude that PlayerSpriteKasua can't be instantiated.

仙气飘飘 2024-08-15 23:21:26

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

You need to redeclare the members of SpriteBase that you are going to implement in PlayerSpriteKasua in the declaration of PlayerSpriteKasua in PlayerSpriteKasua.h.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文