我一直在为循环列表编写一个自定义类,称为 CList。我一直基于很久以前完成的一项家庭作业,其中有很多复制粘贴,所以我不完全记得我写过的所有作品。
无论如何,简单地尝试包含 .h 文件会导致我在 using namespace std 行上出现错误:
main.cpp:11: error: expected unqualified-id before ';' token
以及指向我的代码中的函数的两个错误:
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
这是有问题的功能:
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);
}
我真的不明白这里的错误是什么。我告诉该函数返回 CList 的 CIterator,并表示该函数是 CList 类的一部分。这就是我阅读该行时所理解的
typename CList<T>::CIterator<T> CList<T>::push(T const& v)
为什么明明 T 是模板,它却认为 CIterator 是模板?我只是困惑。