问题
I'm getting a compile error that I can't figure out. It says:
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
Can anyone help?
#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;
}
回答1:
Your syntax is slightly off. You need:
template <class TYPE> Queue<TYPE>::Queue()
{
...
}
template <TYPE>
Queue<TYPE>& operator=(Queue<TYPE> other) { ... }
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) { .... }
};
Also, it is not a good idea to put using namespace std in a header file. In fact, it is not really a good idea to put it anywhere.
来源:https://stackoverflow.com/questions/16683838/invalid-use-of-template-name-queue-without-an-argument-list