Search code examples
c++templatesiteratortypename

C++ Non-Template Used as Template - Nested Template Classes


I know people have asked similar questions in this area, but I can not seem to find a solution to my exact problem. I have a LinkedList and LinkedListIterator class structured as so.

template <typename T>
class LinkedList
{
public:
  template <typename NodeType>
  class LinkedListIterator
  {
    public:
      const LinkedListIterator<NodeType>& operator++();
  }
};

I am trying to implement the operator++() function in a separate file and my attempt is as follows.

template <typename T>
template <typename NodeType>
const typename LinkedList<T>::LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
 // Implementation here
}

Now when I try to use this class, my compiler throws an error saying non-template ‘LinkedListIterator’ used as template. Now I know I have to use typename in the definition of this function since I have dependent scopes occurring. But I can not seem to be able to fix this. I do not understand how this is seen as non-template.


Solution

  • Add template keyword before LinkedListIterator so compiler knows it is a template and not e.g. a field

    template <typename T>
    template <typename NodeType>
    const typename LinkedList<T>::template LinkedListIterator<NodeType>&
    LinkedList<T>::LinkedListIterator<NodeType>::operator++()
    {
      // Implementation here                                                                                                                                                                                                                    
    }