2009-10-23 48 views
6

我在MSVC++ 2008中的问题,在VS2008引发此编译错误:C++ - “成员函数未声明”中派生

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

现在,有什么困惑我的是,渲染()的定义,但在一个继承的类中。

类定义是这样的:

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::,智能感知弹出列表PlayerSpriteBase和SpriteBase蛮好的所有成员,但在编译就像我说的失败以上。

是否有任何特定的原因,我得到这个错误?

PlayerSpriteBase.cpp是空的,至今还没有任何东西。

SpriteBase.cpp有大量的用于SpriteBase函数定义,并使用相同的格式PlayerSpriteKasua.cpp:

void SpriteBase::died() { 
    return; 
} 

就是一个例子。

回答

16

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

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

...或者你是否忽略了这一点,以减少你的文章?

+0

这是这里的问题。我不知道C++自学了它。谢谢! – Sukasa 2009-10-23 21:14:51

+5

好! 请注意,您并不需要在这里再指定“虚拟”(如果没有它,它就可以正常工作),但留下它是一种很好的做法,因为它告诉谁在读取代码时这些方法是从其中一个基类继承的。 – Ludovic 2009-10-23 21:18:21

+2

我来自C#背景,这是疯了。它打破了DRY,特别是如果你有很多策略来做同样的事情。 – 2014-12-06 14:36:08

2

您需要在您的类定义中为PlayerSpriteKasua :: render()提供一个声明。否则,包括您的PlayerSpriteKasua.h在内的其他翻译单元将无法判断您是否提供了定义,并且将被迫断定PlayerSpriteKasua无法实例化。

2

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