错误:. .未在此范围内声明
error: ... was not declared in this scope
我正在处理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;
}
相关文章:
- 错误:未在此范围内声明'reverse'
- 错误:"imread"未在此范围内声明
- 未在此范围内声明错误 'xy'
- Socklen_t未在此范围内声明
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- "Main"已在当前范围内声明
- c++ 变量在宏的扩展中没有在这个范围内声明
- C++ 在编译过程中 strtok 函数 Eclipse 说没有在范围内声明?
- 'num1'未在此范围内声明
- 如何找到在本地范围内声明的变量的地址?
- NT状态未在此范围内声明
- 变量未在此范围内声明 数组线性搜索
- 包含文件中的 Typdef "未在此范围内声明"
- 'ClassName'和'ClassInstance'未在此范围内声明
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 代码作为 C 文件工作,但不作为C++文件,错误:'__builtin_types_compatible_p'未在此范围内声明
- 如何修复错误:"vreinterpretq_u32_f64"未在此范围内声明 - 在Android上使用Eigen构建
- 如何修复C++中的"arrStud未在此范围内声明"错误
- 错误:"SHGetKnownFolderPath"未在此范围内声明
- 错误:'Sprite'未在此范围内声明