Question

[Solved] No default constructor exists for class error

Some simple code:

class Thing {
public:
    int num;
    Thing(int num) { 
        this->num = num; 
    }
};

class Stuff {
public:
    Thing thing;  // an instance of thing is declared here but it cannot construct it
    Stuff(Thing thing) {
        this->thing = thing;
    }
};

int main() {
    Thing thing = Thing(5);
    Stuff stuff = Stuff(thing);
}

So here, I’m trying to work out how I should be taking a new instance of Thing in the constructor of Stuff without pointing to it as I want Stuff to hold its own copy. Of course, I can’t declare thing like I have above because it’s trying to initialise it.

How can I get around this problem of assigning a new object copy to a class’ variable through its constructor?

Exact error is:

In constructor 'Stuff::Stuff(Thing)':
error: no matching function for call to 'Thing::Thing()'
  Stuff(Thing thing){ this->thing = thing; }

candidate expects 1 argument, 0 provided
Enquirer: Jazcash

||

Solution #1:

The problem is here:

Stuff(Thing thing) {
    this->thing = thing;
}

By the time you enter the constructor’s body, the compiler will have already initialized your object’s data members. But it can’t initialize thing because it does not have a default constructor.

The solution is to tell the compiler how to initialize it by using an initlizer list.

Stuff(Thing thing) : thing(thing) {
    // Nothing left to do.
}

This is less typing, cleaner code and more efficient. (More efficient, because if the variable is going to be initialized anyway, then why initialize it with an unwanted value first just to assign another one as quickly as you can? Of course, since your current code doesn’t even compile, “more efficient” is a somewhat dubious statement, here.)

Respondent: 5gon12eder

Solution #2:

Initialize thing member in Stuff with initializer list:

class Stuff {
public:
    Thing thing;  // an instance of thing is declared here but it cannot construct it
    Stuff(Thing thing): thing(thing) { }
};
Respondent: mty

Solution #3:

The only constructor for Thing takes an int parameter.

Thus, when you declare:

Thing thing;

With no arguments, how does it know what num should be?

As the error states, there is 1 expected, but none provided. You need to either add a default constructor:

Thing::Thing(void) : num(0){};

or alternatively modify the argument:

Thing::Thing(int num=0)

In both cases I have used 0, but this should be whatever you think num should be ‘by default’, when nothing is specified.

Without this, Stuff‘s Thing must have a value to initialise it; this is supported via the initialisation list, which you neglected to use in your Thing::Thing (see above), and as a side-note your this->num is not necessary, num is sufficient.

Stuff::Stuff(Thing inThing) : thing(inThing){};
Respondent: OJFord

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy