派生类函数
class Base
{
protected:
int data;
public:
virtual int getData() { return data; }
virtual void setData(int value) { data = value; }
};
class Child : protected Base
{
public:
void setData(int value)
{
Base::setData(value);
cout << "Data is set.\n";
}
};
class Worker
{
private:
Child obj;
public:
void setGlobalData(int val)
{
obj.setData(val); // This is normal
}
int getGlobalData()
{
return obj.getData(); // Line 140, Error
}
};
使用 Worker 类编译文件时出错:
Base.hpp: In member function ‘int Worker::getGlobalData()’:
Base.hpp:22:19: error: ‘virtual int Base::getData()’ is inaccessible
Worker.cpp:140:34: error: within this context
Worker.cpp:140:34: error: ‘Base’ is not an accessible base of ‘Child’
class Base
{
protected:
int data;
public:
virtual int getData() { return data; }
virtual void setData(int value) { data = value; }
};
class Child : protected Base
{
public:
void setData(int value)
{
Base::setData(value);
cout << "Data is set.\n";
}
};
class Worker
{
private:
Child obj;
public:
void setGlobalData(int val)
{
obj.setData(val); // This is normal
}
int getGlobalData()
{
return obj.getData(); // Line 140, Error
}
};
Error during compiling of file with Worker class:
Base.hpp: In member function ‘int Worker::getGlobalData()’:
Base.hpp:22:19: error: ‘virtual int Base::getData()’ is inaccessible
Worker.cpp:140:34: error: within this context
Worker.cpp:140:34: error: ‘Base’ is not an accessible base of ‘Child’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您真的将其设为公共基类吗?
否则它是私有的,并且您会收到与您所遇到的类似的错误,即:
Did you actually make it a public base class?
Otherwise it's private, and you get errors similar to what you have, namely:
这编译:
This compiles:
除了类->阶级与美德 -> virtual 你的代码完全没问题。绝对没有什么问题。因为从字面上看,由于拼写错误,这不会编译,我怀疑您的原始代码有点不同,因为从
Base
派生Child
是私有的,或者getData
是私有的。Except for clas -> class and virtua -> virtual your code is perfectly fine. There is absolutely nothing wrong with it. Since literally this doesn't compile because of the typos I suspect your original code was a bit different in that the derivation of
Child
fromBase
was private or thatgetData
was private.class Worker
不是clas Worker
在
Worker
类中,obj.setData(val);
正在尝试访问私有Child
类的成员。class Worker
notclas Worker
In
Worker
class,obj.setData(val);
is attempting to access a private member ofChild
class.由于当前现有版本的代码通过
protected
继承将Child
子类化为Base
,因此public
函数位于>Base
在Child
中成为受保护
,因此Worker
无法通过Child
对象访问它们。Since the current existing version of the code has
Child
subclassingBase
viaprotected
inheritance, thepublic
functions inBase
becomeprotected
inChild
, soWorker
can not access them viaChild
object.Child
类继承了Base
类作为protected
,因此成员函数Child::getData()
不可公开访问给Child
的客户。正如这里其他人所说,将
Base
的继承更改为public
是解决此问题的一种方法。另请注意,将
Child
对象转换为Base
类型也会使该对象的Base::getData()
函数可公开访问。Class
Child
inherits classBase
asprotected
, so the member functionChild::getData()
is not publically accessible to clients ofChild
.As everyone else here said, changing the inheritance of
Base
topublic
is one way to fix it.Also note that casting your
Child
object to typeBase
also makes the functionBase::getData()
of the object publically accessible.