c++ - 没有参数列表的模板名称队列的无效使用

标签 c++ compiler-errors

我遇到了一个我无法弄清楚的编译错误。它说:

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/

相关文章:

c++ - 在 linux ubuntu 服务器 11.10 (vps) 上使用 asio 使用 C++ 进行编译

c++ - 将基类 2 参数构造函数调用到子类 1 参数构造函数中

vb.net - 继续获取: Debug error BC31019 : Unable to write to output file 'path/form.exe System Error &H80070005& VB.NET

c++-cli - C++/CLI : How to override Equal method of Object class

c++ - 带链表的快速排序

c++ - 在 C++ 中打印元组内的对

c++ - 错误: 'member' is private within this context - namespace

c++ - 体系结构 x86_64 : . .. "_main"的 undefined symbol ,引用自: implicit entry/start for main executable

带有 {} 的 C 宏

c++ - 如何修复对 vtable 的 undefined reference ?