views:

388

answers:

3

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.

+1  A: 

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.

David Seiler
+1  A: 

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

GBegen
+6  A: 

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?

Ludovic
This is the problem right here. I did not know that about C++ having taught myself it. Thanks!
Sukasa
Good!Note that you don't really have to specify "virtual" again here (it would work fine without it) but it's good practice to leave it, as it tells whoever's reading your code that these methods are inherited from one of the base classes.
Ludovic