2012-11-09 92 views
4

請問任何人,請解釋可能導致此錯誤的原因?錯誤:無效的基類C++

Error: Invalid base class 

我有其中一人是從第二派生兩類:

#if !defined(_CGROUND_H) 
#define _CGROUND_H 

#include "stdafx.h" 
#include "CGameObject.h" 


class CGround : public CGameObject // CGameObject is said to be "invalid base class" 
{ 
private: 
    bool m_bBlocked; 
    bool m_bFluid; 
    bool m_bWalkable; 

public: 
    bool draw(); 

    CGround(); 
    CGround(int id, std::string name, std::string description, std::string graphics[], bool bBlocked, bool bFluid, bool bWalkable); 
    ~CGround(void); 
}; 

#endif //_CGROUND_H 

而且CGameObject看起來是這樣的:

#if !defined(_CGAMEOBJECT_H) 
#define _CGAMEOBJECT_H 

#include "stdafx.h" 

class CGameObject 
{ 
protected: 
    int m_id; 
    std::string m_name; 
    std::string m_description; 
    std::string m_graphics[]; 

public: 
    virtual bool draw(); 

    CGameObject() {} 
    CGameObject(int id, std::string name, std::string description, std::string graphics) {} 

    virtual ~CGameObject(void); 
}; 

#endif //_CGAMEOBJECT_H 

我試圖清理我的項目,但徒勞。

+2

猜測,但'std :: string m_graphics [];'不是標準的C++。如果它意味着我認爲它的意思,那麼它會造成一個無效的基類。我建議用'std :: vector m_graphics;'替換。 – john

+0

@john m_graphics []是完全有效的標準C++。 –

+0

@john它幫助,謝謝。請把它作爲anwser輸入,我會將其標記爲最好的。 :) – dziwna

回答

4

定義數組(std::string m_graphics[])而不指定其大小作爲類的成員是無效的。 C++需要事先知道類實例的大小,這就是爲什麼你不能繼承它的原因,因爲C++不會在運行時知道在內存中繼承類的成員可用。
您可以修復類定義中數組的大小,也可以使用指針並將其分配到堆上,或者使用vector<string>而不是數組。