1

I'm coding a simple LinkedList class that is a child class to an abstract List class. After finishing the List class I started to code the LinkedList class, and I noticed that the compiler would not let me access the protected variables from the members of the List class directly, even though LinkedList is a child class of List. When I do not add the "this->" pointer before one of List's variables, I get an error that says the variable is not declared in the scope. I'm sure there is a simple explanation for this, but I can't figure out why the two protected variables in my List class and its members are not accessible from the members of the LinkedList class.

List:

template<typename T>
class List {
    protected:
        unsigned int numElements;
        Node<T>* head;
    public:
        List();
        ~List();
        virtual void append(T x) = 0;
        virtual T remove() = 0;
        virtual bool isEmpty();
};

template<typename T>
List<T>::List() {
    numElements = 0;
    head = NULL;
}

template<typename T>
List<T>::~List() {
    while(NULL != head) {
        Node<T>* newHead = head->getNext();
        delete head;
        head = newHead;
    }
}

template<typename T>
bool List<T>::isEmpty() {
    if (numElements)
        return false;
    return true;
}

LinkedList with one member example:

template<typename T>
class LinkedList: public List<T> {
    public:
        void append(T x);
        T remove();
        void append_tail(T x);
        void insert(T x, int pos);
        T remove_at(int pos);
        void print();
        int getNumElements();
};

template<typename T>
void LinkedList<T>::append(T x) {
    // create a new node with its next as the head
    Node<T>* appendNode = new Node<T>(x, this->head);
    // set the head to this new node
    this->head = appendNode;
    this->numElements++;
}

Node class that is included (not very relevant)

#ifndef NODE_H
#define NODE_H

template<typename T>
class Node {
    private:
        T value;
        Node * next;
    public:

        Node(T v, Node * n);
        T getValue() const;
        Node * getNext() const;
        void setNext(Node * p);
};


template<typename T>
Node<T>::Node(T v, Node * n) {
    value = v;
    next = n;
}


template<typename T>
T Node<T>::getValue() const {
    return value;
}


template<typename T>
Node<T>* Node<T>::getNext() const {
    return next;
}

template<typename T>
void Node<T>::setNext(Node * n) {
    this->next = n;
}

#endif

jkulskis
  • 124
  • 1
  • 4
  • Hi, perhaps this is scoping related? – jspcal Apr 24 '19 at 19:41
  • What's the purpose of `List`? Your class hierarchy is very strange, more to the point, it is not necessary to do things this way if implementing a linked list. You wouldn't have come across your issue if you coded a linked list in the traditional way (one template class with an internal Node class). – PaulMcKenzie Apr 24 '19 at 19:44
  • Hi, I see now that this is from the class being a template base class, thanks for the response. Also @PaulMcKenzie I know it’s a bit strange, but that’s because it’s for an assignment which does not give any flexibility in choosing the class hierarchy. I think this selection of classes was assigned since we just went over abstract classes, and the assignment is supposed to incorporate abstract classes. – jkulskis Apr 24 '19 at 19:50
  • Yes, but why do so many teachers (or institutions) insist on teaching an aspect of C++ in such a roundabout way? There are plenty of real-world examples where usage of abstract classes makes sense -- this is not one of them. – PaulMcKenzie Apr 24 '19 at 19:52
  • That is a question for which I have no answer to, but I do agree that it is an annoying part of learning c++ in college – jkulskis Apr 24 '19 at 20:07

0 Answers0