的隱式實例下面是一個模板(隊列),我試着寫:未定義模板
#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
,看看是不是這個問題,但顯然不是,因爲我得到了同樣的錯誤。
我使用XCode與LLVM編譯器4.2。當我切換到GCC我得到更多的錯誤:
template<class> class Node;
得到Declaration of 'struct Queue<int>::Node<int>'
, Node<T&> *El = new Node<T> (NewEl);
得到Invalid use of incomplete type
, 和任何與什麼薩爾瓦多分配處理不能轉換<int&>*
到<int>*
(但刪除引用不會改變任何東西LLVM)。