简体   繁体   中英

C++ Derived Class Chain Constructor Error

All,

I have 3 classes (in C++) in an inheritance chain with default base constructors set for each one. However, the 3rd class in the chain complains about no matching constructor for the 1st one. Below is the code for the constructors:

class Base
{
    protected:
        int a, b;
    public:
        Base::Base(int first, int second)
        {
            this->a = first;
            this->b = second;
        }
}

class Second : protected Base
{
    protected:
        int c;
    public:
        Second::Second(int first, int second, int third = 2) : Base(first, second)
        {
            this->c = third;
        }
}

class Final : protected Second
{
    private:
        int d;
    public:
        Final::Final(int first, int second, int third = 2) : Second(first, second, third)
        {
        }
}

At compile time I get the error

"In constructor Final::Final(int first, int second, int third)

no matching call to Base()"

Why is this trying to call Base() instead of Base(first, second)?

This may help you this is how I would structure this class hierarchy.

class Base {
protected: 
    int m_a, m_b;

public:
    Base( int first, int second );
    virtual ~Base(){}
};

Base::Base( int first, int second ) :
m_a( first ),
m_b( second ) 
{}

class Second : protected Base {
protected:
    int m_c;
public:
    Second( int first, int second, int third = 2 );
    virtual ~Second(){}
};

Second::Second( int first, int second, int third ) :
Base( first, second ),
m_c( third )
{}

class Final : protected Second {
private:
    int m_d;
public:
   Final( int first, int second, int third = 2 );
   virtual ~Final(){}
};

Final::Final( int first, int second, int third ) :
Second( first, second, third )
{}

If you want to keep your definitions within the class and not define them outside of the class declaration then try this

class Base {
protected: 
    int m_a, m_b;

public:
    Base( int first, int second ) : m_a( first ), m_b( second ) {}
    virtual ~Base(){}
};

class Second : protected Base {
protected:
    int m_c;
public:
    Second( int first, int second, int third = 2 ) : 
    Base( first, second ),
    m_c( third ) {}

    virtual ~Second(){}
};

class Final : protected Second {
private:
    int m_d;
public:
   Final( int first, int second, int third = 2 ) :
   Second( first, second, third ) {}

   virtual ~Final(){}
};

It is good practice to make sure your destructors are declared as virtual when using inheritance. You only need to use the class scope resolution operator if you are defining the Constructor or Functions outside of the class, and to set basic member variables you do not need to use the this-> pointer operator, you can use the initializer list as I have shown when defining your constructors.

First, you are missing ; at the end of class definition. Also, while implementing member functions inside class a class, you can't use score operator :: , it's invalid in C++. You may find This useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM