1

The dummyNode declaration variable was working well until i wrote iterator class as nested, now it gives me an error invalid use of non-static data member, 'linkedList::dummyNode' c++, if i removed iterator class it works well template

class linkedList
{

private:
    listNode<T> * head, *tail;
    listNode<T> *  dummyNode = new listNode<T>;
    int sz = 0;
public:
    class iterator
    {
    public:
        iterator()
        {
            itrNode = head;
        }
        void operator ++ ()
        {
            try{
                if(itrNode == dummyNode)
                    throw "Sorry this is the end of the list\n";
                else
                {
                    itrNode = itrNode->next;
                }

            }catch(const char * error)
            {
                cerr << error;
            }
        }
        T& operator *()
        {
            return *(itrNode->value);
        }
        void operator -- ();
    private:
        listNode<T> * itrNode;

    };
    linkedList();
    ~linkedList();
    linkedList(T value, int initial_size);
    iterator begin();


};
shrouk mansour
  • 381
  • 3
  • 16
  • Your constructor in the inner class references `head`, which is a data member of the outer class. You should instead make a function `linkedlist::begin` which returns the proper iterator. If you need direct constructors for the iterator, it needs a `linkedlist` as an argument. – jwimberley Apr 16 '17 at 13:18
  • sorry I can't understand where's the problem with that ?! and i didn't understand the solution as well, would you please write the code ?! – shrouk mansour Apr 16 '17 at 13:22

1 Answers1

0

A C++ nested class do not share data with its outer class -- if there were the case, each instance of the inner class would be in a one-to-one relationship with a corresponding instance of an outer class and would thus add no extra functionality. Instead, the main purposes of nested classes are:

  1. Namespace meaning: linkedlist::iterator is more meaningful than linkedlist_iterator
  2. Depending on the C++ version standard, a nested class has extra visibility of the member objects of instances of the outer class
  3. Maybe others that I'm not aware of

The biggest problem in your code is the constructor of the iterator

    iterator()
    {
        itrNode = head;
    }

The data head means nothing in the context of the inner class, because its a data member of a linkedlist object -- again, the iterator does not belong to an instance of the linkedlist class. Instead the constructor should be something like

    iterator(linkedlist<T>& list)
    {
        itrNode = list.head; // Might not work in C++03
    }

There might be other changes you need to make regarding the templatization (I'm not sure; I haven't made nested templated classes before). Also, this type of constructor for the linked list should work, but doesn't follow standard paradigms. More standard would be adding functions

iterator linkedlist::begin();
iterator linkedlist::end();

to the linkedlist class. Even better would be to create begin(linkedlist&) and end(linkedlist&) functions. This is explained in this following SO post.

Community
  • 1
  • 1
jwimberley
  • 1,696
  • 11
  • 24
  • what about if(itrNode == dummyNode) i think i should do the same but i don't know how ?! – shrouk mansour Apr 16 '17 at 13:57
  • You're right that you'll have to fix that too. And you do know how; you already have a private itrNode entry in the `iterator` class and you just need to do the same for the dummyNode. I'll admit I'm not sure what your dummy node does, but I guess those details are in the implementation of `linkedlist`'s constructors and member function. – jwimberley Apr 16 '17 at 14:09