[英]C++ multilevel inheritance - Calling Base Constructor Error
So I have a fairly complex programm that I dont whant to go into right now. 所以我有一个相当复杂的程序,我不想现在就进入。 I will include a toy example of the same process and then go over it in more detail.
我将包含一个相同过程的玩具示例,然后对其进行详细介绍。
In my Programm I encounter the Error constructor for 'Hunter' must explicitly initialize the base class 'WorldObject' which does not have a default constructor
with multilevel inheritance: WorldObject
-> Creature
-> Hunter
. 在我的程序中,我遇到了
constructor for 'Hunter' must explicitly initialize the base class 'WorldObject' which does not have a default constructor
的Error constructor for 'Hunter' must explicitly initialize the base class 'WorldObject' which does not have a default constructor
多级继承constructor for 'Hunter' must explicitly initialize the base class 'WorldObject' which does not have a default constructor
: WorldObject
> Creature
> Hunter
。
To recreate the same structure I made the following: 要重新创建相同的结构,我做了以下工作:
class Base
{
protected:
int a;
public:
Base(int a): a(a) { print("Base contructed"); }
~Base() { print("Base destroyed"); }
virtual void printData() = 0;
};
class Derived1 : public Base
{
protected:
int b;
public:
Derived1(int a, int b): b(b), Base(a) { print("Derived1 contructed"); }
~Derived1() { print("Derived1 destroyed"); }
};
class Derived2 : public Derived1
{
protected:
int c;
public:
Derived2(int a, int b, int c) : c(c), Derived1(a, b) { print("Derived2 contructed"); }
~Derived2() { print("Derived2 destroyed"); }
virtual void printData(){ //... }
};
Here, the constructor of Derived2 class created Derived1 via the initializer list and this in turn constructs Base "indirectly". 在这里,Derived2类的构造函数通过初始化器列表创建了Derived1,这反过来又间接构造了Base。 This works like I expected.
这像我预期的那样工作。
However, in my complex Code, the Hunter class needs to explicitly call the WorldObject constructor. 但是,在我的复杂代码中,Hunter类需要显式调用WorldObject构造函数。 This looks like:
看起来像:
Hunter(sf::Texture &texture, float x, float y, sf::Font& font) :
WorldObject(texture,x, y, font),
Creature(texture, x, y, font)
{ //... }
Here, The Creature constructor just passes every argument to the WorldObject constructor. 在这里,Creature构造函数只是将每个参数传递给WorldObject构造函数。 WorldObject only has this constructor:
WorldObject仅具有以下构造函数:
WorldObject(sf::Texture& texture, float x, float y, sf::Font& font) : m_sprite(texture)
{ //... }
and the used Creature constructor looks like this: 使用的Creature构造函数如下所示:
Creature(sf::Texture &texture, float x, float y, sf::Font& font) :
WorldObject(texture, x, y, font),
NN(n_input_units, n_hidden_units, n_output_units)
{ //... }
Why do I need to initialize both WorldObject and Creature directly in my Programm, but in the toy example it works without the explicit Base constructor? 为什么我需要在程序中直接初始化WorldObject和Creature,但是在玩具示例中,它无需显式的Base构造函数即可工作?
(( The pre-compiler is also complaining that there is no default constructor for WorldObject, and on compiling the above error appears)) ((预编译器还抱怨WorldObject没有默认构造函数,并且在编译时出现上述错误))
I guess that in your complex code, Hunter
directly inherits from WorldObject
and not indirectly via Creature
. 我想在您的复杂代码中,
Hunter
直接继承自WorldObject
而不是间接通过Creature
继承。 If Creature
inherits WorldObject
, it will never be necessary for Hunter
to pass any parameters to WorldObject
. 如果
Creature
继承了WorldObject
,则Hunter
永远不需要将任何参数传递给WorldObject
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.