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

error: ... was not declared in this scope

本文关键字:范围内 声明 错误      更新时间:2023-10-16

我正在处理c++中的队列,我遇到了一个奇怪的编译错误:

Queue.h: 37:49:错误:预期","或"…"& lt;"牌
Queue.h:在复制构造函数"Queue::Queue(Queue&) "中":
Queue.h:39:11: error: "other"未在此作用域中声明
Queue.h:在全局作用域中:
Queue.h:42:72:错误:在"<"之前期望","或"…"
Queue.h:在成员函数Queue&队列::操作符= (const Queue&)":
Queue.h:44:11: error: "other"未在此作用域中声明

有人能帮忙吗?

#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;
}

你把&放错地方了:

更新
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;
}

查看注释以查看更改。也正如其他人指出的,将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;
}