I have a question regarding classes and inheritance. I am a student, but the question does not relate to homework or an assignment. I am doing C++ data structures exercises, and I am still learning some of the more basic concepts.
I have a parent class:
1 2 3 4 5 6 7 8 9 10 11 12 13
template<class T>
class linkedListType
{
public:
protected:
int count;
nodeType<T> *first;
nodeType<T> *last;
private:
};
Derived class:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "linkedListType.h"
template<class T>
class orderedLinkedList: public linkedListType<T>
{
public:
void mergeList(orderedLinkedList<T> &list1, orderedLinkedList<t> &list2)
{
first = list1.first;
...
}
private:
};
There is more code in my mergeList() function, but I included the line that is giving me problems. The error that my CodeBlocks compiler is giving me is that 'first' was not declared in this scope. Strangely enough, if I change it to this->first, the error disappears.
1. Why does it not recognise 'first'?
2. Why would this->first work at all? Is the 'this' object a smart pointer?
You can use this->first or linkedListType<T>::first or orderedLinkedList::first. I don't know exactly why this is necessary but it has something to do with the use of templates.