2012-11-09 57 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>而不是数组。