0

I don't know why it doesn't work.

template <typename E>
class SearchTree {
public:
    class Iterator {
             Iterator& operator++();
    };
};

template <typename E>
Iterator& SearchTree<E>::Iterator::operator++() {}

warning C4346 : 'iterator' Dependent name is not a type

error C2061 : Syntax error: identifier 'iterator'

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Elinz
  • 19
  • 2
  • 1
    [This code does not cause the error you are asking about.](https://coliru.stacked-crooked.com/a/90c441f6d55099ea) Please show a [mcve]. – Code-Apprentice Jun 06 '19 at 23:44
  • 3
    Fyi, once you fix that, the operator is still worthless (it's private). Regardless, I suspect this may be eventually helpful: [Where and why do I have to put the “template” and “typename” keywords?](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – WhozCraig Jun 06 '19 at 23:44

3 Answers3

3

You can use trailing return type to fix this:

template <typename E>
auto SearchTree<E>::Iterator::operator++() -> Iterator& {}

In the trailing return type, types can be inside the scope of the class. Before the SearchTree<E>:: name lookup don't see inside the class.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
2

It's a question about scope.

With

template <typename E>
Iterator& SearchTree<E>::Iterator::operator++() {}

when you use the symbol Iterator for the return type, that type isn't known in that scope. You have to specify its scope:

template <typename E>
typename SearchTree<E>::Iterator& SearchTree<E>::Iterator::operator++() {}

As mentioned in comments and other answers, you can also use a trailing return type. This is possible because then the scope is known.


On a related note, and why I added the typename keyword for the return type, see Where and why do I have to put the “template” and “typename” keywords?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You want to fully specify the return value:

template <typename E>
SearchTree<E>::Iterator& SearchTree<E>::Iterator::operator++() {}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54