C++ - “未声明成员函数"在派生类中 [英] C++ - "Member function not declared" in derived class

查看:22
本文介绍了C++ - “未声明成员函数"在派生类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

现在,让我感到困惑的是 render() 已定义,但在继承的类中.

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

类定义如下:

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

因此,SpriteBase.h 的精简版本如下:

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 是这样的:

PlayerSpriteBase.h is this:

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

最后,PlayerSpriteKasua.h 是这样的:

And finally, PlayerSpriteKasua.h is this:

class PlayerSpriteKasua : public PlayerSpriteBase
{
public:
};

我知道其中还没有成员,但这仅仅是因为我还没有添加他们.PlayerSpriteBase 也是如此;还有其他事情要做.

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.

PlayerSpriteKasua.cpp 中的代码是这样的:

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

当我输入 void PlayerSpriteKasua:: 时,Intellisense 会弹出列出 PlayerSpriteBase 和 SpriteBase 的所有成员就好了,但是在编译时它会像我上面说的那样失败.

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 是空的,目前还没有任何内容.

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

SpriteBase.cpp 有大量的 SpriteBase 函数定义,格式与 PlayerSpriteKasua.cpp 相同:

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

void SpriteBase::died() {
  return;
}

是一个例子.

推荐答案

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

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?

这篇关于C++ - “未声明成员函数"在派生类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆