因此,在我的派生类OrderedList的头文件中,我通过告诉编译器使用using List<DataType>::examplefunction;
的基类方法继承了我之前创建的List类的一些功能。所有未被覆盖的函数以及以上述方式声明的函数都是OrderedList的私有成员。
因此,当我运行程序时,我在Microsoft Visual Studio中获取错误:
错误C2248:'OrderedList :: examplefunction':无法访问类'OrderedList'中声明的私有成员
examplefunction在基类List中是公共的。
以下是我正在使用的具体示例:
在OrderedList.h中,
private:
using List<DataType>::remove;
在List.h中,
public:
void remove () throw ( logic_error );
其中remove在List.cpp中,
void List<DataType>::remove () throw ( logic_error )
{ // Do some operations//
}
我的OrderedList头文件中的声明也是这样的:
#include "List.cpp"
template < typename DataType, typename KeyType >
class OrderedList : public List<DataType>
如果有人能够了解导致该问题的原因,那将非常感激。
答案 0 :(得分:1)
如果您的List类中的exampleFunction是私有的,则您的OrderedList类将无法访问它。改为使其受到保护。见Private and Protected Members : C++
答案 1 :(得分:0)
将继承的方法移动到public,并将OrderedList头文件中受保护的数据成员工作。
<强>更新强>
所以这是大约一年前的事了。然而,现在看起来显而易见。给出的说明是将base class
(List
)的继承方法声明为private
,但是在main
中由教科书作者提供(用于测试目的)正在调用一些继承的方法。在private
中创建的OrderedList
实例无法调用main
。
我们的导师后来更正了说明,但有时作为学生,您可以密切关注。