错误:...未在此范围内声明

时间:2013-05-22 06:26:37

标签: c++ compiler-errors

我正在使用c ++编写队列,我遇到了一些奇怪的编译错误:

  

Queue.h:37:49:错误:在“<”之前预期“,”或“...”令牌
  Queue.h:在复制构造函数“Queue :: Queue(Queue&)”中:
  Queue.h:39:11:错误:在此范围内未声明“其他”   Queue.h:全球范围:
  Queue.h:42:72:错误:在“<”之前预期“,”或“...”令牌
  Queue.h:在成员函数“Queue& Queue :: operator =(const Queue&)”中:
  Queue.h:44:11:错误:“其他”未在此范围内声明

有人可以帮忙吗?

#if !defined QUEUE_SIZE
#define QUEUE_SIZE 30
#endif

template <class TYPE> class Queue
{
 private:
  TYPE *array;
 public:
  Queue(Queue& other);
  Queue();
  ~Queue();
  Queue& operator=(const Queue& other);
  TYPE pushAndPop(TYPE x);
};

template <class TYPE> Queue<TYPE>::Queue()
{
  array=new TYPE[QUEUE_SIZE];
}

template <class TYPE> Queue<TYPE>::~Queue()
{
  delete [] array;
}

template <class TYPE> TYPE Queue<TYPE>::pushAndPop(TYPE other)
{
  TYPE item = array[0];
  for(int x = 0; x<QUEUE_SIZE-1; x++){
    array[x]= array[x+1];
  }
  array[QUEUE_SIZE-1] = other;
  return item;
}

template <class TYPE> Queue<TYPE>:: Queue(Queue&<TYPE> other)
{
  array = other.array;
}

template <class TYPE> Queue<TYPE>& Queue<TYPE>:: operator=(const Queue&<TYPE> other)
{
  array = other->array;
}

2 个答案:

答案 0 :(得分:2)

您将&amp; 放在错误的位置:

更新

template <class TYPE> Queue<TYPE>:: Queue(Queue&<TYPE> other)
                                               ^^
{
  array = other.array;
}

template <class TYPE> Queue<TYPE>& Queue<TYPE>:: operator=(const Queue&<TYPE> other)
                                                                      ^^^
{
  array = other->array;    // other is a reference, not pointer
               ^^^
}

template <class TYPE> Queue<TYPE>:: Queue(Queue<TYPE>& other)
{
  //array = other.array;
}

template <class TYPE> Queue<TYPE>& Queue<TYPE>:: operator=(const Queue<TYPE>& other)
{
  // check that it's not self-assign
  // make sure you release current memory
  // allocate new memory
  // copy over content of array
  // array = other.array;
}

答案 1 :(得分:1)

查看评论以查看更改。另外,其他人指出将Queue&<TYPE>更改为Queue<TYPE>&

template <class TYPE> class Queue
{
 private:
  TYPE *array;
 public:
  Queue(const Queue& other); // Take a const reference instead
  Queue();
  ~Queue();
  Queue& operator=(const Queue& other);
  TYPE pushAndPop(TYPE x);
};

template <class TYPE> Queue<TYPE>::Queue()
{
  array=new TYPE[QUEUE_SIZE];
}

template <class TYPE> Queue<TYPE>::~Queue()
{
  delete [] array;
  array = NULL; // Be safe
}

template <class TYPE> TYPE Queue<TYPE>::pushAndPop(TYPE other)
{
  TYPE item = array[0];
  for(int x = 0; x<QUEUE_SIZE-1; x++){
    array[x]= array[x+1];
  }
  array[QUEUE_SIZE-1] = other;
  return item;
}

template <class TYPE> Queue<TYPE>::Queue(const Queue<TYPE>& other)
{
  array=new TYPE[QUEUE_SIZE];

  for(int x = 0; x<QUEUE_SIZE; x++){
    array[x]= other.array[x]; // Deep copy of array is required
  }
}

template <class TYPE> Queue<TYPE>& Queue<TYPE>:: operator=(const Queue<TYPE>& other)
{
   // this piece of code is repeated in copy-ctor, good candidate to extract as a method  
   for(int x = 0; x<QUEUE_SIZE; x++){
    array[x]= other.array[x]; // Deep copy of array is required, 
  }

  return *this;
}