Derek W
Derek W

Reputation: 10046

Issue having access to private member in a class

So in the header file of my derived class OrderedList I am inheriting some of the functionality of my previously created List class by telling the compiler to use a base class method by using List<DataType>::examplefunction;. All the functions which are not being overrided and that are being declared in the aforementioned way are private members of OrderedList.

So when I run my program, I obtain the error in Microsoft Visual Studio of:

error C2248: 'OrderedList::examplefunction' : cannot access private member declared in class 'OrderedList'

examplefunction is public in the base class List.

Here is a concrete example of what I am working with:

In OrderedList.h,

private: 
using List<DataType>::remove;

In List.h,

public:
void remove () throw ( logic_error );

And where remove is in List.cpp as,

void List<DataType>::remove () throw ( logic_error ) 
{ // Do some operations// 
}

Also the declaration in my OrderedList header file is like this:

#include "List.cpp"

template < typename DataType, typename KeyType >
class OrderedList : public List<DataType>

If anyone could enlighten me to what is causing the issue that would be much appreciated.

Upvotes: 0

Views: 123

Answers (2)

Derek W
Derek W

Reputation: 10046

Moving the inherited methods to public and the data members to protected in the OrderedList header file worked.

Update

So this was about a year ago. However, it seems so blatantly obvious now. The instructions that were given said to declare the inherited methods from the base class (List) as private, but in main which was provided by the author of the textbook (for testing purposes) some of the inherited methods were being called. Which while being private were not able to be called by the instance of OrderedList that was being created in main.

The instructions were later corrected by our instructor, but sometimes as a student you can follow along to closely.

Upvotes: 0

Nathan Fig
Nathan Fig

Reputation: 15119

If exampleFunction is private in your List class, your OrderedList class will not be able to access it. Make it protected instead. See Private and Protected Members : C++

Upvotes: 1

Related Questions