1

I have defined a template container Tree<T>, with two member-class iterators : const_iterator and iterator

Now I would like to add non member comparison operators:

template<typename T>
bool operator==(Tree<T>::const_iterator a, Tree<T>::iterator b)
{
    return a.ptr() == b.ptr();
}

But I have the compilation error:

declaration of 'operator==' as non-function

Why? Is this due to the template?

galinette
  • 8,896
  • 2
  • 36
  • 87
  • Even with missing `typename`, your overload cannot be really used as `T` cannot be deduced. The way to go is to add a friend `operator ==` inside `Tree::const_iterator`. – Jarod42 Feb 10 '17 at 12:48

1 Answers1

4

You need to use typename for the dependent name here, e.g.

template<typename T>
bool operator==(typename Tree<T>::const_iterator a, typename Tree<T>::iterator b)
//              ~~~~~~~~                            ~~~~~~~~
{
    return a.ptr() == b.ptr();
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405