[英]Error: Invalid base class C++
Could anyone, please, explain what can cause this error? 任何人都可以解释导致此错误的原因吗?
Error: Invalid base class
I've got two classes where one of them is derived from second: 我有两个类,其中一个是从第二个派生的:
#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
And CGameObject looks like this: 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
I tried cleaning my project but in vain. 我试图清理我的项目,但徒劳无功。
It is not valid to define an array ( std::string m_graphics[]
) without specifying its size as member of a class. 如果未将数组的大小指定为类的成员,则定义数组(
std::string m_graphics[]
)是无效的。 C++ needs to know the size of a class instance in advance, and this is why you cannot inherit from it as C++ won't know at runtime where in the memory the members of the inheriting class will be available. C ++需要事先知道一个类实例的大小,这就是为什么您不能从它继承的原因,因为C ++在运行时将不知道该继承类的成员在内存中的可用位置。
You can either fix the size of the array in the class definition or use a pointer and allocate it on the heap or use a vector<string>
instead of the array. 您可以在类定义中固定数组的大小,也可以使用指针在堆上分配它,或者使用
vector<string>
代替数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.