-1

我正在使用 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;
}
4

2 回答 2

2

你把&放在错误的地方:

更新

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;
}
于 2013-05-22T06:32:50.673 回答
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;
}
于 2013-05-22T06:37:32.563 回答