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

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

问题描述

我在MSVC ++ 2008中有一个问题,其中VS2008抛出这个编译错误:

 错误C2509:'render':member函数未在'PlayerSpriteKasua'中声明

现在,令人困惑的是render



类定义的工作方式如下:

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

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

  class SpriteBase {
public:
//变量=============== ============================
-snip-
//主要功能=====
virtual void think()= 0; //调用每一帧以允许精灵处理事件并对播放器做出反应。
virtual void render(long long ScreenX,long long ScreenY)= 0; //调用每个帧渲染精灵。
//各种可覆盖而不是服务/事件函数===
virtual void died(); //当sprite被外部或通过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//一次性包含所有需要的文件

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

是一个示例。

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

...或者您是否省略了此操作以缩短您的帖子?


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.

解决方案

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天全站免登陆