二进制树编译错误的迭代器

 星星之火 发布于 2023-02-09 13:25

我收到以下编译错误:

错误:从'const IntervalST '到'IntervalST '的无效转换[-fpermissive]

当我编译我的代码.代码很大但这里是相关部分:

IntervalST类:

template  class intervalST_const_iterator;

template  class IntervalST
{
private:
    Interval *root;

    //friend class intervalST_const_iterator;//allow the iterator class to access the private section of intervalST

    bool isRed(Interval *interval);
    Interval *rotateLeft(Interval *h);
    Interval *rotateRight(Interval *h);
    Interval *put(Interval *h,Key lo, Key hi, Key val);
    Interval *moveRedLeft(Interval *h);
    Interval *moveRedRight(Interval *h);
    Interval *deleteMin(Interval *h, Key hi);
    Interval *balance(Interval *h);
    Interval *remove(Interval *h, Key lo, Key hi);
    Interval *min(Interval *h);
    Interval *addDuplicate(Interval *h, Key hi);
    Interval *removeDuplicate(Interval *h, Key low, Key hi);
    Interval *getPointerToKey(Key low);

    void flipColors(Interval *h);
    void destroy(Interval *h);
    void printTree(Interval *h, int indent);
    Key maxVal(Interval *h);
    int size(Interval *h);
    bool isBST(Interval *x, Key min, Key max);
    inline bool isBST(){return isBST(root,0,0);}
    bool isSizeConsistent(Interval *x);
    inline bool isSizeConsistent(){return isSizeConsistent(root);}
    bool is23(Interval *x);
    inline bool is23(){return is23(root);}
    bool isBalanced();
    bool isBalanced(Interval *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 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 IntervalST::const_iterator IntervalST::begin() const
{
return const_iterator(root,this);//<-----------code complaining here
}

template  typename IntervalST::const_iterator IntervalST::end() const
{
return const_iterator(NULL,this);
}

这是迭代器类:

template  class intervalST_const_iterator
{
//friend class IntervalST;

private:
    Interval *interval;
    IntervalST *tree;

public:
    intervalST_const_iterator(Interval *p, IntervalST *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 operator *() const
    {
        return *(this->interval);
    }
    intervalST_const_iterator & left() const
    {
        return (interval = interval->left);
    }
    intervalST_const_iterator & right() const
    {
        return (interval = interval->right);
    }
};

为什么会这样?以及如何解决?令人惊讶的是,end()方法的实现与begin()方法的实现几乎相同.谢谢你的帮助

1 个回答
  • 您的会员功能Interval<Key>::begin(),已标记const,因此任何使用this也必须const.在这种情况下,迭代器类的构造函数接受指向非const Interval<Key>对象的指针,因此this不允许传递给此.您应该将const关键字添加到类中的所有Interval<Key>指针intervalST_const_iterator,从构造函数参数开始.

    由于类似的原因,你的left()right()成员函数也intervalST_const_iterator将无法编译,因为它们也标记为const,但它们修改interval了迭代器的字段.

    请注意,在C++模板库的约定中,a 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
    

    2023-02-09 13:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有