错误:在'<'标记之前预期的unqualified-id

时间:2011-12-21 20:12:44

标签: c++ templates

我正在尝试制作某种模板Queue类。看起来没问题,但我在同一行得到2个错误,我无法弄清楚原因。错误出现在实现文件.cpp中,我试图给出析构函数的定义。以下是该类头文件的代码:

#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 个答案:

答案 0 :(得分:2)

请在获取两条错误消息的行上使用“template”而不是“typename”!我发现大多数情况下,错误位置的未识别关键字或真实关键字通常会产生类似于未定义类型的错误,其后的下一个符号会导致错误。