尝试从派生结构继承时"Invalid base class"错误 (C++)
"Invalid base class" error when trying to inherit from derived struct (C++)
我有一个"基本;结构,一个";NPC";结构派生自";基本";。一切都很好。但是当我尝试创建一个名为";PC";从";NPC";结构,我得到一个错误:";无效基类";。怎么了?是否不能从派生结构创建结构?
struct Base
{
char* name = 0;
int MaxHP = 0;
int CurrHP = 0;
};
struct NPC : Base
{
int gold = 0;
int stats[];
};
struct PC : NPC // I get the error here
{
unsigned int ID = 0;
};
当你写:
struct NPC : Base
{
int gold = 0;
int stats[]; //NOT VALID, this is a definition and size must be known
};
这是无效,因为来自cppreference:
以下任何上下文都要求类型T是完整的:
- 类型为T的非静态类数据成员的声明
但是非静态数据成员stats
的类型不完整,因此出现错误。
是的,结构可以从类继承。类关键字和结构关键字之间的区别只是默认的私有/公共说明符的变化。-->这里你需要指定public关键字!
struct Base
{
char* name = 0;
int MaxHP = 0;
int CurrHP = 0;
};
struct NPC : public Base
{
int gold = 0;
int stats[];
};
struct PC : public NPC
unsigned int ID = 0;
};
相关文章:
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Visual Studio MFC C++ "CFormView" "base class"下拉列表中缺少
- 尝试应用肖恩父母谈话"inheritance is the base class of evil"中的模式
- 有没有办法在Python中做"template base class"?
- 通过"a pointer of the base class"访问未在基类中声明的子类的方法或属性(动态)
- 多态性>错误:'no known conversion from derived class to base class'
- Clang - 删除某些文件的"conversion function converting <A> to base class <B> will never be used"
- 禁止从模板类的库标头中"Base class ‘class X’ should be explicitly initialized in the copy constructor"
- "virtual base class in the case of multilevel inheritance"有意义吗
- 无法将"member pointer to derived class"转换为"member pointer to base class"
- 派生类'no type named my_data in <base class>'中的错误
- "polymorphic base class"是C++标准的一部分吗?
- 子构造函数无法识别基类的成员:平均值,西格玛"is not a nonstatic data member or base class"
- "Base class undefined."
- 如何在C++实现继承并解决错误"parent class is not accessible base of child class"?
- Qt 序列化。QDataStream & operator<< for base class QPolygonF
- 关于肖恩·帕伦特谈话的澄清 "Inheritance is the base class of evil"
- int 和 double 的均匀随机分布"base class"?
- 硬件:C++错误"trying to initialize Abstract base class"