Error : base class constructor must explicitly initialize parent class constructor

后端 未结 5 758
傲寒
傲寒 2021-02-02 07:22

I am new to c++. When I try to compile the code below , I get this error

constructor for \'child\' must explicitly initialize the base class \'parent\' whic

5条回答
  •  盖世英雄少女心
    2021-02-02 07:47

    When you initialize an object of a derived class, the base class part has to be constructed first. If you don't initialize it yourself in the derived class' constructor by calling one of its constructors, the compiler will attempt use the default constructor of the base class. In your case the default constructor is not defined because you already provided a custom constructor.

    To solve this you will either have to provide a default constructor for the base class or simply call its constructor in the derived class' constructor's initializer list:

    child::child(int a) : parent(a)
     {
     }
    

提交回复
热议问题