【发布时间】:2013-03-23 05:08:27
【问题描述】:
这是我正在尝试编写的模板(队列):
#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>'。我究竟做错了什么?阅读this post 后,我尝试将int 更改为const int 以查看是否是问题所在,但显然不是,因为我遇到了同样的错误。
我正在使用带有 LLVM 编译器 4.2 的 XCode。当我切换到 GCC 时,我收到更多错误:
template<class> class Node; 得到Declaration of 'struct Queue<int>::Node<int>',
Node<T&> *El = new Node<T> (NewEl); 得到 Invalid use of incomplete type,
并且任何处理将任何内容分配给 El 的内容都无法将 <int&>* 转换为 <int>*(但删除引用不会改变 LLVM 的任何内容)。
【问题讨论】: