c ++使用模板时<class t =“”>错误:..不是类

时间:2015-10-15 14:22:47

标签: c++

我写了一个名为Queue的类。当我尝试构建项目时,出现编译时错误。

.h文件:

template<class Queue_entry>
 class MyQueue {
     public:
    MyQueue();
    bool empty() const;
    // add entry in the tail of the queue
    Error_code append(Queue_entry &x);
    // throw the entry of the front
    Error_code serve();
    // get the front entry of the queue
    Error_code retrieve(Queue_entry &x) const;
 protected:
    Queue_entry entry[MAXQUEUE];
    int count;
    int front, rear;
};

.cpp文件中出现错误:

  

MyQueue.cpp:17:1:'MyQueue'不是类,命名空间或枚举

我不知道什么是错的,但当我将模板更改为

#define Queue_entry int

它可以成功运行。

1 个答案:

答案 0 :(得分:6)

当问我的同学时,我知道它应该是

template <class Queue_entry>
MyQueue<Queue_entry>::MyQueue() {}

所以这个问题就解决了。我应该记住格式。