开发者

C++ - "Member function not declared" in derived class

开发者 https://www.devze.com 2022-12-09 13:29 出处:网络
I have a problem in MSVC++ 2008 where VS2008 is throwing this compile error: error C2509: \'render\' : member function not declared in \'PlayerSpriteKasua\'

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:

cla开发者_StackOverflowss 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消