c++ - 错误 : expected unqualified-id before ‘<’ token

标签 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 ===||

最佳答案

请在收到两条错误消息的行中使用"template"而不是"typename"!我发现很多时候,一个未识别的关键字或者一个真正的关键字在错误的地方经常会给出类似于未定义类型的错误,它后面的下一个符号会导致错误。

关于c++ - 错误 : expected unqualified-id before ‘<’ token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8595735/

相关文章:

c++ - 如何通过函数 C++ 链接数组值

c++ - Objective-C:如果我在 ARC 的函数中返回一个 C++ 对象,它会创建一个拷贝吗?我是否必须手动释放该拷贝?

c++ - "onEachSubelement(...)"C++ 方法

c++ - 显式实例化模板的动态使用

c++ - 是否可以概括一个将 STL 容器作为参数的函数?

c++ - pimpl 习语如何减少依赖性?

c++ - 嵌套的 cmake 库

c++ - 如何确定将使用哪个模板

c++ - 使用模板化是否为带有自定义键的 unordered_map/set 制作了一个好的模式

c++ - 使用 std::remove_if() 时没有可行的重载 '='