1

I'm writing a tree that follows this header:

class TreeLetters {
private:
    Letter* root;

public:
    // ...

    Letter* Root () const;

    class iterator {
    private:
        Letter* it;

    public:
        iterator ();
        iterator (Letter* letter);
        iterator (const iterator & other);

        iterator begin ();
        iterator end   ();

        // ...
    };
};

As simple as it is, I'm getting an error with the iterator's begin() method, which is as follows:

TreeLetters::iterator TreeLetters::iterator :: begin () {
    return iterator(root); // invalid use of non-static data member 'TreeLetters:root'
}

I've been reading some old questions and the only solution that has at least swept the problem under the rug for me is making the root static, which creates the greater problem of not being able to have more than one tree.

I've tried making the root protected and adding a friend class TreeLetters to the private members of the iterator, but nothing has worked. I'm also working with C++11.

What am I doing wrong?

Groctel
  • 130
  • 11
  • Looks to me like begin() and end() should be in TreeLetters not in iterator. begin/methods usually return iterators but are not part of the iterator class rather its container. – Borgleader Dec 24 '19 at 13:26
  • You're right, moving the definitions to TreeLetters solved it! Add it as an answer if you like :) – Groctel Dec 24 '19 at 13:31

1 Answers1

3

In my point of view, the iterator must have the whole tree (which is a pointer to tree root), not a reference to current letter. You can refer to this question. I hope that helps solve your problem.

Mohammed Deifallah
  • 1,290
  • 1
  • 10
  • 25