c++ – No suitable default constructor

Question:

There are two classes, the din inherits another, the public inheritance type. On compilation throws the following error: No suitable default constructor. Tell me what the problem is, two header files below.

Integer.h

    class Integer {
private:
    int *_mass;
    int _system;
    int _size;
public:
    Integer(const int *mass, const int system, const int size);
    Integer(const Integer &);
    ~Integer() {
        delete[] _mass;
        _mass = nullptr;
    }
    Integer & operator=(const Integer &);

IntegerCalc.h

class IntegerCalc: public Integer {
private:
    Integer _a;
    Integer _b;
public:
    IntegerCalc(Integer &a, Integer &b) :
        _a(a), _b(b) {}; // ВОТ ТУТ ВЫДАЁТ ОШИБКУ
    ~IntegerCalc() {};

Answer:

Your IntegerCalc class inherits from Integer – which means any IntegerCalc constructor must call the Integer constructor. Not the constructor of the field you are calling – but the constructor of the base class.

Since you don't call it – the compiler automatically adds a call to the default constructor. It turns out something like this:

public:
    IntegerCalc(Integer &a, Integer &b) :
        Integer(), _a(a), _b(b) {};

But there is no default constructor in the base class! Hence the error.

How to fix such a mistake depends on the task. I don't understand very well what you wanted to do with your code.

UPD

The calculator subclass inherits the numbers class in order to be able to use operators and methods.

This should not be done. In order to "use operators and methods", you must use a simple friend , not inheritance. Better yet, don't make a separate interface for the calculator. Why can't the calculator use open methods?

Scroll to Top