Reputation: 898
I've been writing a custom class for a circular list, called CList. I've been basing it off a homework assignment I completed a long while ago with a lot of copy paste so I don't exactly remember exactly everything I've been writing works.
Anyway, simply trying to include the .h file causes me the error on the using namespace std line:
main.cpp:11: error: expected unqualified-id before ';' token
As well as two errors pointing to a function in my code:
In file included from main.cpp:9:
CList.h:119: error: non-template 'CIterator' used as template
CList.h:119: note: use 'CList<T>::template CIterator' to indicate that it is a template
This is the function in question:
template <class T>
typename CList<T>::CIterator<T> CList<T>::push(T const& v)
{
size++;
Node<T>* p = new Node<T>(v);
if (this -> size_ == 1)
{
head = p;
tail = p;
p -> next = p;
p -> prev = p;
}
else
{
tail -> next = p;
p -> next = head;
p -> prev = tail;
head -> prev = p;
tail = p;
}
return CIterator(p);
}
I don't really understand what the error here is. I am telling the function to return a CIterator of a CList, and am denoting that this function is part of the CList class. That is what I understand when I read the line
typename CList<T>::CIterator<T> CList<T>::push(T const& v)
Why does it think that CIterator is the template when clearly T is the template? I'm just confused.
Upvotes: 3
Views: 229
Reputation: 154025
The error message states that CIterator<T>
isn't considered a template. This is because it is a dependent name and the compiler can only really determine at instantiation time that it, indeed, is a nested template. The fix for the problem is to tell the compiler that it actually is a template:
template <class T>
typename CList<T>::template CIterator<T> CList<T>::push(T const& v)
{
...
}
The reason the compiler doesn't know if CList<T>::CIterator<T>
is a template is that the class CList<X>
could be specialized for some type X
to not have a nested template inside. However, the compiler only finds out about these specializations when the template is instantiated.
Upvotes: 1