Reputation: 1737
I'm trying to write a relatively deep class heirarchy and my compiler keeps throwing "no matching function for call to [default constructor for bass class]". Here's the scenario:
Class A {
A(int);//note, no default constructor
}
Class B : public A {
B(int i, int j) : A(i), someMemberVariable(j) {}
int someMemberVariable;
}
Class C : public B {
C(int k, int l) : B(k, l) {}
}
and the compiler throws the error on the line for the constructor of class C saying "no matching function for call to A::A()" and tells me to use A::A(int).
I understand that I don't have a default constructor for class A, and the compiler is getting confused when I try to subclass a subclass. However, what I don't understand is why. I have used an initialization list to avoid exactly that. If I only use classes 2-levels deep then it works just fine, but the third class gives me the error. What am I doing wrong here?
Upvotes: 0
Views: 426
Reputation: 1155
As people commented you just needed to make the constructors public and then your code had some formatting issues:
class A
{
public:
A(int a) : blah(a) {}; //note, no default constructor
int blah;
};
class B : public A
{
public:
B(int i, int j) : A(i), someMemberVariable(j) {}
int someMemberVariable;
};
class C : public B
{
public:
C(int k, int l) : B(k, l) {}
};
int main( void )
{
C c(5,4);
return 0;
}
This code compiles cleanly and does what you want.
Upvotes: 3