2011-12-21 696 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”的行上获取两条错误消息!我发现大多数情况下,一个未识别的关键字或一个真正的关键字在错误的地方往往会给出类似于未定义类型的错误,而后面的符号会导致错误。

相关问题