invalid use of template-name Queue without an argument list

痴心易碎 提交于 2021-01-28 12:06:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!