0

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.

nick2225
  • 527
  • 5
  • 17

1 Answers1

3

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                                                                                                                                                                                                                    
}
Łukasz Ślusarczyk
  • 1,775
  • 11
  • 20
  • Thanks that seemed to do the trick. But I have a follow up question. I thought that was the point of including the typename keyword? Do you mind explaining why I need both the typename keyword and template and when I use each individually? – nick2225 Aug 27 '20 at 08:59
  • Compiler explains a bit when you omit _typename_: `error: need ‘typename’ before ‘LinkedList::LinkedListIterator’ because ‘LinkedList’ is a dependent scope const LinkedList::template LinkedListIterator&` – Łukasz Ślusarczyk Aug 27 '20 at 09:01