0

Okay so I have a class, LinkedList, with a nested class, LinkedListIterator. Within LinkedListIterator's methods I reference the private fields of LinkedList. Which I thought was legal. But I get the error:

from this location

every time I reference them.

And I get corresponding error messages on the fields in the enclosing class:

invalid use of non-static data member 'LinkedList<int>::tail'

Any idea why? The relevant code is below:

template<class T>
class LinkedList {

    private:
        //Data Fields-----------------//
        /*
         * The head of the list, sentinel node, empty.
         */
        Node<T>* head;
        /*
         * The tail end of the list, sentinel node, empty.
         */
        Node<T>* tail;
        /*
         * Number of elements in the LinkedList.
         */
        int size;

    class LinkedListIterator: public Iterator<T> {

            bool add(T element) {

                //If the iterator is not pointing at the tail node.
                if(current != tail) {

                    Node<T>* newNode = new Node<T>(element);
                    current->join(newNode->join(current->split()));

                    //Move current to the newly inserted node so that
                        //on the next call to next() the node after the
                        //newly inserted one becomes the current target of
                        //the iterator.
                    current = current->next;

                    size++;

                    return true;
                }

                return false;
            }
Ethan
  • 1,206
  • 3
  • 21
  • 39
  • 1
    When asking questions that involves compilation or linker errors, it helps to post _all_ of the error messages, as complete as possible. I.e. just copy-paste them verbatim into the question. – Some programmer dude Aug 23 '12 at 06:14
  • See here http://stackoverflow.com/questions/486099/can-inner-classes-access-private-variables – jogojapan Aug 23 '12 at 06:14
  • @jogojapan completely different question, but the answer show that the inner class has a reference to an outer class instance, which is a way of going about solving this problem. – Luchian Grigore Aug 23 '12 at 06:16
  • @LuchianGrigore In particular, that other question (and its answers) explains that instances of inner classes can't use members of the outer class as if they were their own members. Hence the basic underlying assumption of the question above is wrong. (To be clear, I didn't say the other question is a duplicate or anything.) – jogojapan Aug 23 '12 at 06:21

1 Answers1

2

You can't just use non-static members like that. I think the following example will clear things out:

LinkedList<int>::LinkedListIterator it;
it.add(1);

What would current and tail be inside the method? There's no instance of LinkedList to speak of, so those members don't even exist yet.

I'm not saying make the members static, that would be wrong, but re-think your approach.

Look into how std iterators are.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625