我遇到了一个我无法弄清楚的编译错误。它说:
Queue.h:18:23: 错误:在没有参数列表的情况下无效使用模板名称“队列” Queue.h:23:23: 错误:在没有参数列表的情况下无效使用模板名称“队列”
有人能帮忙吗?
#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;
}
最佳答案
您的语法略有偏差。你需要:
template <class TYPE> Queue<TYPE>::Queue()
{
...
}
template <TYPE>
Queue<TYPE>& operator=(Queue<TYPE> other) { ... }
虽然请注意,在大多数情况下您可能应该通过 const 引用传递(当然不是通过非 const 引用传递):
template <TYPE>
Queue<TYPE>& operator=(const Queue<TYPE>& other) { ... }
或者,您可以内联实现:
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) { .... }
};
此外,它是 not a good idea to put using namespace std
in a header file .其实就是not really a good idea to put it anywhere .
关于c++ - 没有参数列表的模板名称队列的无效使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683838/