inheritance - C++ initialization list in second derrived class -
i'm trying write relatively deep class heirarchy , compiler keeps throwing "no matching function call [default constructor bass class]". here's scenario:
class { a(int);//note, no default constructor } class b : public { b(int i, int j) : a(i), somemembervariable(j) {} int somemembervariable; } class c : public b { c(int k, int l) : b(k, l) {} }
and compiler throws error on line constructor of class c saying "no matching function call a::a()" , tells me use a::a(int).
i understand don't have default constructor class a, , compiler getting confused when try subclass subclass. however, don't understand why. have used initialization list avoid that. if use classes 2-levels deep works fine, third class gives me error. doing wrong here?
as people commented needed make constructors public , code had formatting issues:
class { public: a(int a) : blah(a) {}; //note, no default constructor int blah; }; class b : public { 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 , want.
Comments
Post a Comment