5

请任何人解释一下可能导致此错误的原因?

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

我尝试清理我的项目但徒劳无功。

4

1 回答 1

7

std::string m_graphics[]定义数组 ( ) 而不将其大小指定为类的成员是无效的。C++ 需要提前知道类实例的大小,这就是为什么不能从它继承的原因,因为 C++ 在运行时不知道继承类的成员在内存中的哪个位置可用。
您可以在类定义中固定数组的大小,也可以使用指针并在堆上分配它,或者使用 avector<string>代替数组。

于 2012-11-09T13:14:59.217 回答