Error : base class constructor must explicitly initialize parent class constructor

后端 未结 5 689
傲寒
傲寒 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条回答
  •  梦毁少年i
    2021-02-02 07:32

    The parent class has an explicit constructor, so compiler will not add an implicit 'empty' constructor to it. Additionally your constructor has a parameter, so compiler can not generate an implicit call to it. That's why you must do it explicitly.

    This way:

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

提交回复
热议问题