没有参数列表的模板名称队列的无效使用 [英] invalid use of template-name Queue without an argument list
问题描述
我收到了一个我不知道的编译错误.它说:
I'm getting a compile error that I can't figure out. It says:
Queue.h:18:23:错误:在没有参数列表的情况下无效使用模板名称"Queue"Queue.h:23:23:错误:在没有参数列表的情况下无效使用了模板名称"Queue"
Queue.h:18:23: error: invalid use of template-name ‘Queue’ without an argument list Queue.h:23:23: error: invalid use of template-name ‘Queue’ without an argument list
任何人都可以帮忙吗?
#if !defined QUEUE_SIZE
#define QUEUE_SIZE 30
#endif
using namespace std;
template <class TYPE> class Queue
{
private:
TYPE *array;
public:
Queue(Queue& other);
Queue();
~Queue();
Queue& operator=(Queue other);
TYPE pushAndPop(TYPE x);
};
template <class TYPE> Queue::Queue()
{
array=new TYPE[size];
}
template <class TYPE> Queue::~Queue()
{
delete [] array;
}
推荐答案
您的语法略有不同.您需要:
Your syntax is slightly off. You need:
template <class TYPE> Queue<TYPE>::Queue()
{
...
}
template <TYPE>
Queue<TYPE>& operator=(Queue<TYPE> other) { ... }
尽管请注意,在大多数情况下,您可能应该通过const引用传递(肯定不是通过非const引用):
Although note that you probably should be passing by const reference in most cases (certainly not by non-const reference):
template <TYPE>
Queue<TYPE>& operator=(const Queue<TYPE>& other) { ... }
或者,您可以内嵌实现:
Alternatively, you can inline the implementations:
template <class TYPE> class Queue
{
private:
TYPE *array;
public:
Queue(Queue& other);
Queue() { array = new TYPE[size];}
~Queue() { delete [] array; }
Queue& operator=(Queue other) { .... }
TYPE pushAndPop(TYPE x) { .... }
};
此外, =" =">使用命名空间std放置不是一个好主意
>在头文件中.实际上,将其放置在任何地方并不是一个好主意.
这篇关于没有参数列表的模板名称队列的无效使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!