error C2614: 'ChildClass' : illegal member initialization: 'var1' is not a base or member

前端 未结 2 408
无人共我
无人共我 2020-12-31 07:20

I am getting the following error in C++:

error C2614: \'ChildClass\' : illegal member initialization: \'var1\' is not a base or member

2条回答
  •  时光说笑
    2020-12-31 07:37

    You can't initialize a member of a base class, only of the current class. Use a parameterized constructor in the base class.

    Class Base 
    {
      protected:
         int var1;
         Base( int var ) : var1(var)
         {}
      public:
         Base()
         {
            var1=0;
         }
    };
    
    class Child:public Base
    {
          int chld;
       public: 
          Child():Base(0)
          {
             chld=1;
          }
    };
    

提交回复
热议问题