Reputation: 659
I have a question related to inherotance.
I declare a class and then declare a child class as below
`Producer::Producer(Pool<string>* p, string id) {
mPool = p; // protected members
mId = id;
}
ProduceCond::ProduceCond(Pool<string>* p, string id) {
Producer(p, id);
}
class Producer{
}
class ProduceCond : public Producer, public ThreadSubject {
}
`
Though I have called right parent constructor in the child constructor I receive an error
ProduceCond.cpp:10:52: error: no matching function for call to ‘Producer::Producer()’
Can someone tell me why I receive this error although I use correct constructor format of the parent?
Upvotes: 0
Views: 52
Reputation: 45410
To initialize sub-object(base) which has no default constructor, you need to call it through member initializer list:
ProduceCond::ProduceCond(Pool<string>* p, string id) : Producer(p, id) {}
Or you could provide a default constructor which will be called implicitly by sub-class constructor
Producer() : mPool(std::nullptr) { }
Also you have to use member initializer in below conditions:
1 You must (at least, in pre-C++11) use this form to initialize a nonstatic const data
member.
2 You must use this form to initialize a reference data member.
Upvotes: 1
Reputation: 227390
You need to use the constructor initialization list:
ProduceCond::ProduceCond(Pool<string>* p, string id) : Producer(p, id)
{
....
}
Otherwise, you are default constructing a Producer
(which you can't, because it has no default constructor), then doing something strange in the body of the constructor.
Upvotes: 2