Non-visibility of protected data members!

Why is this happening?
What?

Does anyone know why I can't access the protected data members of the base class linkedListType in the member functions of my derived class unorderedLinkedList??

The skeleton of what I have is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

template<class elemType>
struct nodeType                   //structure of the node
{
    elemType info;
    nodeType<elemType> *link;
};


template<class elemType>   
class linkedListType
{
public:
    const linkedListType<elemType>& operator=(const linkedListType<elemType>&);
    linkedListType();                                                    
    linkedListType(const linkedListType<elemType>& otherList);         
    ~linkedListType();    
    virtual bool search(const elemType& searchItem) const = 0;
    virtual void insertFirst(const elemType& newItem) = 0;
    virtual void insertLast(const elemType& newItem) = 0;
    virtual void deleteNode(const elemType& deleteItem) = 0;  
        .
        .
        .											   

protected:                             
    int count; 			
    nodeType<elemType>* first;          
    nodeType<elemType>* last;             
    
private:
    void copyList(const linkedListType<elemType>& otherList);	
};


template<class elemType>
class unorderedLinkedList: public linkedListType<elemType>
{
public:
	bool search(const elemType& searchItem) const;
	void insertFirst(const elemType& newItem); 
	void insertLast(const elemType& newItem);
	void deleteNode(const elemType& deleteItem); 
	void sortGuestByLastName();            
           .
           .
           .
};



The definition of my sort function is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
template<class elemType>
void unorderedLinkedList<elemType>::sortGuestByLastName()
{
     int i, j;
     int smallestIndex;
     string firstName_1;           
     string firstName_2;           
     string lastName_1;           
     string lastName_2;              
     elemType temp;             
        
        
     nodeType<elemType> *smallestNode;
     nodeType<elemType> *current;
     nodeType<elemType> *trailCurrent;
     
     for (trailCurrent = first; trailCurrent != last; trailCurrent = trailCurrent->link)      
     {
         smallestNode = trailCurrent;
         for (current = trailCurrent->link; current != NULL; current = current->link)          
         {
             current->info.getName(firstName_1, lastName_1);      //fn getname(), whose definition is unshown, is defined
                                                                  //for a class whose object is the data part of the node
             smallestNode->info.getName(firstName_2, lastName_2);
             
             if (lastName_1 < lastName_2)
                  smallestNode = current;
         }  
         
         //trailCurrent->info.swap(smallestNode->info);        
         temp = trailCurrent->info;
         trailCurrent->info = smallestNode->info; 
         smallestNode->info = temp; 
     }  
}



When I tried to compile the program, I got the message below regarding the sort function:

In member function `void unorderedLinkedList<elemType>::sortGuestByLastName()':
error: `first' undeclared (first use this function)
error: (Each undeclared identifier is reported only once for each function it appears in.)
error: `last' undeclared (first use this function)


It's baffling why I couldn't access the protected member variables first and last of the base class from the member functions of the derived class!! This pattern happened across several other derived class functions, also. Why is this so?
Did you try to access it by explicitely stating that you need members?

for (trailCurrent = this->first //...
Thx MiiNiPaa!

Your suggestion solved the problem. But I'm still astonished that the members of the derived class could not access the protected member variables of the base class without using the this pointer, in this case. I have written programs where such protected data members are visible/accessible. It's not like the data members are private; for then, I would understand why they are inaccessible. But, they are protected!! This conflicts with the concepts of inheritance I've read anywhere.

Do you have an explanation for this?
Last edited on
Templates have slightly different rules for finding names, it is likely that without this the compiler doesn't know to look in the base class.
I'll check them out; thx guys!
Topic archived. No new replies allowed.