[英]using struct template in template class implementation
我正在嘗試學習c ++中的模板用法。 我創建了一個結構節點,我在隊列類實現中使用但是我收到了編譯器錯誤:錯誤“在成員函數bool MyQueue :: add(T data)中qnode之前的預期類型說明符
#include <iostream>
using namespace std;
template <typename T>
struct qnode {
qnode* Node;
T data;
};
template <class T>
class MyQueue {
qnode<T>* front;
qnode<T>* end;
public:
MyQueue() {
front=NULL;
end=NULL;
}
bool add (T n);
T get(void);
bool empty(void)
{
if ( front == NULL)
return true;
else
return false;
}
size_t size(void)
{
}
};
template <typename T>
bool MyQueue<T>::add ( T n)
{
qnode<T>* temp = new qnode;
temp->data = n;
temp->Node = NULL;
if ( front == NULL )
{
cout << "Adding front qnode " << endl;
front = end= temp;
// front->Node = end;
return true;
}
cout << "Adding qnode " << endl;
end->Node = temp;
end=temp;
//delete temp;
return true;
}
我期待着在這種嵌套實現中如何解析模板參數的一個很好的解釋。
您的new
語法有錯誤。
qnode<T>* temp = new qnode;
應該
qnode<T>* temp = new qnode<T>();
請記住,沒有模板參數的模板類對編譯器沒有意義。 無論何時鍵入qnode(在初始聲明之后),您還需要鍵入其模板參數!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.