我在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;
}
就是一個例子。
這是這裏的問題。我不知道C++自學了它。謝謝! – Sukasa 2009-10-23 21:14:51
好! 請注意,您並不需要在這裏再指定「虛擬」(如果沒有它,它就可以正常工作),但留下它是一種很好的做法,因爲它告訴誰在讀取代碼時這些方法是從其中一個基類繼承的。 – Ludovic 2009-10-23 21:18:21
我來自C#背景,這是瘋了。它打破了DRY,特別是如果你有很多策略來做同樣的事情。 – 2014-12-06 14:36:08