当尝试在构造函数中将祖先类型用作参数时,g ++似乎误认为了不存在的默认构造函数的类型。这里发生了什么,有解决办法了吗?
最小示例:
class Outer
{
};
class Middle : Outer
{
public:
Middle(int i):i(i){}
protected:
int i;
};
class Inner : Middle
{
public:
Inner(int i, Outer *o):Middle(i){}
};
g ++输出:
example.h:16:18: error: ‘class Outer Outer::Outer’ is inaccessible within this context
Inner(int i, Outer *o):Middle(i){}
^~~~~
example.h:2:1: note: declared here
{
^
答案 0 :(得分:3)
您需要完全限定Outer
,否则它是不可访问的私有基础:
class Inner : Middle
{
public:
Inner(int i, ::Outer *o):Middle(i){}
};
或者使用protected
/ public
继承链:
class Middle : public Outer
{
public:
Middle(int i):i(i){}
protected:
int i;
};
class Inner : public Middle
{
public:
Inner(int i, Outer *o):Middle(i){}
};