Vote count:
0
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
asked 1 min ago
No default constructor exists for class error
Aucun commentaire:
Enregistrer un commentaire