class base
{
base () { }
virtual ~base () { }
}
class middleBase
{
middleBase () { }
开发者_如何学JAVA middleBase (int param) { }
~middleBase () { }
}
class concrete : public middleBase
{
concrete () { }
concrete (int param) { // process }
~concrete () { // delete something }
}
Error is : undefinded reference to "middleBase::middleBase (int param)"
and is this suitable for best practice for parameterized constructors?
class base
{
public: // constructor should be accessible by derived class
base () { }
virtual ~base () { }
}; // add semicolon
class middleBase : public base // you missed the declaration
{
public:
middleBase () { }
middleBase (int param) { }
virtual ~middleBase () { }
};
class concrete : public middleBase
{
public:
concrete () { }
concrete (int param) : middleBase(param) { /* process */ }
virtual ~concrete () { /* delete something */ }
};
c++ needs semicolons after class definitions:
class { … };
If you don't put access specifier (public/private/protected) before class' member functions or variables, the default is private
specifier for C++ classes. So middleBase
's constructor is private and cannot be access by concrete
class' constructor, causing the error you mentioned.
精彩评论