二叉树编译错误的迭代器
Iterator on binary tree compilation error
我收到以下编译错误:
错误:从"const IntervalST"到"IntervalST"的转换无效 [-允许]
当我编译代码时。代码很大,但以下是相关部分:
类 IntervalST:
template <class Key> class intervalST_const_iterator;
template <class Key> class IntervalST
{
private:
Interval<Key> *root;
//friend class intervalST_const_iterator<Key>;//allow the iterator class to access the private section of intervalST
bool isRed(Interval<Key> *interval);
Interval<Key> *rotateLeft(Interval<Key> *h);
Interval<Key> *rotateRight(Interval<Key> *h);
Interval<Key> *put(Interval<Key> *h,Key lo, Key hi, Key val);
Interval<Key> *moveRedLeft(Interval<Key> *h);
Interval<Key> *moveRedRight(Interval<Key> *h);
Interval<Key> *deleteMin(Interval<Key> *h, Key hi);
Interval<Key> *balance(Interval<Key> *h);
Interval<Key> *remove(Interval<Key> *h, Key lo, Key hi);
Interval<Key> *min(Interval<Key> *h);
Interval<Key> *addDuplicate(Interval<Key> *h, Key hi);
Interval<Key> *removeDuplicate(Interval<Key> *h, Key low, Key hi);
Interval<Key> *getPointerToKey(Key low);
void flipColors(Interval<Key> *h);
void destroy(Interval<Key> *h);
void printTree(Interval<Key> *h, int indent);
Key maxVal(Interval<Key> *h);
int size(Interval<Key> *h);
bool isBST(Interval<Key> *x, Key min, Key max);
inline bool isBST(){return isBST(root,0,0);}
bool isSizeConsistent(Interval<Key> *x);
inline bool isSizeConsistent(){return isSizeConsistent(root);}
bool is23(Interval<Key> *x);
inline bool is23(){return is23(root);}
bool isBalanced();
bool isBalanced(Interval<Key> *x,int black);
int getKeySize(Key low);
int compare(Key a, Key b);
public:
//don't forget to build the constructor
//and overload the =equal operator
typedef intervalST_const_iterator<Key> const_iterator;//const iterator on a tree
const_iterator begin() const;
const_iterator end() const;
IntervalST():root(NULL){};
~IntervalST();
void remove(Key lo, Key hi);
void put(Key lo, Key hi);
inline int size(){return size(root);}
inline bool isEmpty(){return root == NULL;}
void print(int indent = 0);
void check();
};
编译器抱怨类 IntervalST 的 begin() 方法的实现
begin() 和 end() 方法:
template <typename Key> typename IntervalST<Key>::const_iterator IntervalST<Key>::begin() const
{
return const_iterator(root,this);//<-----------code complaining here
}
template <typename Key> typename IntervalST<Key>::const_iterator IntervalST<Key>::end() const
{
return const_iterator(NULL,this);
}
下面是迭代器类:
template <class Key> class intervalST_const_iterator
{
//friend class IntervalST<Key>;
private:
Interval<Key> *interval;
IntervalST<Key> *tree;
public:
intervalST_const_iterator(Interval<Key> *p, IntervalST<Key> *t): interval(p), tree(t){}
bool operator != (const intervalST_const_iterator & other) const
{
return this->interval != other.interval;
}
bool operator == (const intervalST_const_iterator & other) const
{
return this->interval == other.interval;
}
Interval<Key> operator *() const
{
return *(this->interval);
}
intervalST_const_iterator<Key> & left() const
{
return (interval = interval->left);
}
intervalST_const_iterator<Key> & right() const
{
return (interval = interval->right);
}
};
为什么会这样?以及如何解决它?令人惊讶的是,end() 方法的实现与实现几乎相同的 begin() 方法。感谢您的帮助
你的成员函数Interval<Key>::begin()
,标记为const
,所以任何this
的使用也必须const
。在这种情况下,迭代器类的构造函数接受指向非常量Interval<Key>
对象的指针,因此不允许在此处传递this
。应将 const
关键字添加到 intervalST_const_iterator
类中的所有Interval<Key>
指针中,从构造函数参数开始。
出于类似的原因,intervalST_const_iterator
的 left()
和 right()
成员函数也将无法编译,因为它们也被标记为 const,但它们修改了迭代器的 interval
字段。
请注意,在C++模板库的约定中,const_iterator
本身不是常量,但它迭代的容器对象是常量。与允许您添加/删除/修改容器元素的非常量iterator
相反。两个 const/non-const 迭代器本身必须是可变的,以便您可以逐步执行每个元素。
更新:在迭代器类中,成员变量必须是常量:
const Interval<Key> *interval;
const IntervalST<Key> *tree;
然后,迭代器的成员函数还必须使用常量间隔指针:
intervalST_const_iterator(const Interval<Key> *p, const IntervalST<Key> *t)
const Interval<Key> operator *() const
您的intervalST_const_iterator
类构造函数需要指向Interval<Key>
和IntervalST<Key>
的非常量指针:
intervalST_const_iterator(Interval<Key> *p, IntervalST<Key> *t);
在 const 方法中IntervalST
您尝试创建intervalST_const_iterator:
template <typename Key> typename IntervalST<Key>::const_iterator IntervalST<Key>::begin() const
{
return const_iterator(root,this);//<-----------code complaining here
}
this
具有类型 const IntervalST *
,但您尝试将其作为intervalST_const_iterator
期望的构造函数传递IntervalST *
。
- 初始化迭代器错误 C++ 在 GCC 编译器中
- 赛松迭代器错误
- C++ 迭代器错误"does not refer to a value"
- C 功能具有参考参数错误的迭代器错误.寻求解释
- 在 GCC 上自动声明映射迭代器错误
- 指针向量的迭代器错误预期;
- 获取迭代器错误的位置
- std::map<>::迭代器错误
- 迭代器错误从非标量类型转换为非标量类型
- 从 const char* 到迭代器错误的"无已知转换" - 另一个例子
- 类分配上的映射迭代器错误
- C++矢量迭代器错误
- C++ std::vector 迭代器错误
- stl 列表迭代器错误
- 列表迭代器错误c++
- C++ 迭代器错误
- 向量迭代器错误
- 迭代器错误C2440
- 列表的迭代器错误
- 迭代器中的矢量迭代器错误