这是我正在尝试编写的模板(队列):
#include <iostream>
using namespace std;
template <typename T>
class Queue
{
friend ostream& operator<< (ostream &, const Queue<T> & );
private:
template<class> class Node;
Node<T> *front;
Node<T> *back;
public:
Queue() : front(0), back(0) {}
~Queue();
bool Empty()
{
return front == 0;
}
void Push(const T& NewEl)
{
Node<T&> *El = new Node<T> (NewEl);
if (Empty())
front=back=El;
else
{
back-> next = El;
back = El;
}
}
void Pop()
{
if (Empty())
cout << "Очередь пуста." << endl;
else
{
Node<T> *El = front;
front = front -> next;
delete El;
}
}
void Clear()
{
while (! Empty())
Pop();
}
};
template <typename T>
class Node
{
friend class Queue<T>;
public:
Node() {next = 0;}
Node(T nd) {nd=node; next=0;}
T& getsetnode(){return node;}
Node<T>*& getsetnext(){return next;}
private:
T front;
T back;
T node;
Node<T> *next;
};
template <class T> ostream& operator<< (ostream &, const Queue<T> & );
int main()
{
Queue<int> *queueInt = new Queue<int>;
for (int i = 0; i<10; i++)
{
queueInt->Push(i);
cout << "Pushed " << i << endl;
}
if (!queueInt->Empty())
{
queueInt->Pop();
cout << "Pop" << endl;
}
queueInt->Front();
queueInt->Back();
queueInt->Clear();
cout << "Clear" << endl;
return 0;
}
在这些行:
Node<T&> *El = new Node<T> (NewEl);
front = front -> next;
delete El;
我明白了Implicit instantiation of undefined template 'Queue<int>::Node<int>'
。我究竟做错了什么?阅读这篇文章后,我尝试更改int
以const int
查看是否是问题所在,但显然不是,因为我遇到了同样的错误。
我将 XCode 与 LLVM 编译器 4.2 一起使用。当我切换到 GCC 时,我收到更多错误:
template<class> class Node;
gets Declaration of 'struct Queue<int>::Node<int>'
、
Node<T&> *El = new Node<T> (NewEl);
getsInvalid use of incomplete type
和任何处理分配给 El 的任何东西都不能转换<int&>*
为<int>*
(但删除引用不会改变 LLVM 的任何东西)。