2011-12-21 684 views
1

我正在嘗試使某種模板Queue類。這似乎確定,但我得到2錯誤在同一行,我不明白爲什麼。這些錯誤出現在實現文件.cpp中,我試圖給析構函數定義。下面是類的頭文件的代碼:錯誤:在'<'token之前預期的非限定id

#ifndef QUEUETP_H_INCLUDED 
#define QUEUETP_H_INCLUDED 

template <class T> 
class QueueTp 
{ 
    private: 
     struct Node { T item; struct Node * next;}; 
     enum {QSIZE = 10}; 
     //Queue's head 
     Node *head; 
     //Queue's tail 
     Node *tail; 
     int size; 
     int maxsize; 
     QueueTp(const QueueTp & q); 
     QueueTp & operator=(const QueueTp & q) { return *this;} 

    public: 
     QueueTp(): size(0),head(0),tail(0),maxsize(QSIZE) {}; 
     QueueTp(int q = QSIZE): size(0),head(0),tail(0),maxsize(q) {}; 
     ~QueueTp(); 
     bool isEmpty(){return size==0;} 
     bool isFull() {return size==maxsize;} 
     int sizecur() {return size;} 
     bool push(const T& t); 
     bool pop(T& t); 
}; 

#include "QueueTp.cpp" 
#endif // QUEUETP_H_INCLUDED 

,這裏是在執行文件中的析構函數的定義:

#include "QueueTp.h" 
#include <iostream> 

using namespace std; 

typename <class T> //<-<-<- in this line I am getting the two errors 
QueueTp<class T>::~QueueTp() 
{ 
    Node *ptr; 
    cout<<endl<<"Deleting the queue..."; 
    while (head !=NULL) 
    { 
     ptr = head->next; 
     delete head; 
     head = ptr; 
    } 
} 

//......other method definitions 

的錯誤,指出上面和具體的錯誤消息我從編譯器得到的是下面的。

error: expected nested-name-specifier before ‘<’ token| 
error: expected unqualified-id before ‘<’ token| 
||=== Build finished: 2 errors, 12 warnings ===|| 
+1

'模板 QueueTp ::〜QueueTp()' – ildjarn 2011-12-21 20:21:36

+0

我可以刪除莫名其妙的愚蠢問題。我感到尷尬。 – 2011-12-21 20:22:06

+0

是的,你的問題底部應該有一個'刪除'鏈接。 : - ] – ildjarn 2011-12-21 20:22:42

回答

2

請在使用「模板」而不是「typename」的行上獲取兩條錯誤消息!我發現大多數情況下,一個未識別的關鍵字或一個真正的關鍵字在錯誤的地方往往會給出類似於未定義類型的錯誤,而後面的符號會導致錯誤。

相關問題