Question

How do I go about overloading a template class like below?

template <class T>
const_iterator& List<T>::const_iterator::operator++()
{
  current = current->next;
  return *this;
}

template <class T>
const_iterator List<T>::const_iterator::operator++(int)
{
  const_iterator old = *this;
  ++( *this );
  return old;
}

I am getting errors like below:

List.cpp:17: error: expected constructor, destructor, or type conversion before ‘&’ token
List.cpp:23: error: expected constructor, destructor, or type conversion before ‘List’
List.cpp:30: error: expected constructor, destructor, or type conversion before ‘&’ token
List.cpp:35: error: expected constructor, destructor, or type conversion before ‘List’
Was it helpful?

Solution

template <class T>
typename List<T>::const_iterator& List<T>::const_iterator::operator++()

At the time the return type is specified, you're not inside the so-called lexical scope of List<T>. And since there is no type const_iterator in the enclosing scope, you get an error (though that one could manifest itself a little bit better, IMHO).

Another option for C++11 might be a trailing return type:

template<class T>
auto List<T>::const_iterator::operator++()
    -> const_iterator&
{
  // ...
}

However, the best idea would be to just define these things inline in the class itself. Do you have a special reason for the out-of-class definitions?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top