首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有调用‘Tree<int>::operator==(Tree<int>*&,Tree<int>*&) const’的匹配函数

没有调用‘Tree<int>::operator==(Tree<int>*&,Tree<int>*&) const’的匹配函数
EN

Stack Overflow用户
提问于 2014-02-09 15:52:19
回答 1查看 139关注 0票数 0

我有一个通用树实现,我得到了这个错误,编写了一个递归树比较。在第89行,我得到了这个错误:没有调用‘Tree::operator==(Tree&,Tree&) const’的匹配函数--这是我的代码:

代码语言:javascript
复制
#include <iostream>
#include <list>

template<class T> class Tree {
public:
    Tree();
    Tree(const T& pNode);
    virtual ~Tree();

    const T& getNode();
    void setNode(const T& pNode);

    void addChild(Tree<T>* pChild);
    const Tree<T>* getChild(int pIndex);

    const std::list<Tree<T>*>* getChildren();
    void printTree(const Tree<T>* pTree, int pLevel);

    bool operator==(const Tree<T>& other) const;
private:
    T node;
    std::list<Tree<T>*>* children;
};

template<class T> Tree<T>::Tree(const T& pNode) :
        node(pNode), children(nullptr) {
}

template<class T> Tree<T>::Tree() :
        node(T()), children(nullptr) {
}

template<class T> Tree<T>::~Tree() {
    delete children;
}

template<class T> const T& Tree<T>::getNode() {
    return this->node;
}

template<class T> void Tree<T>::setNode(const T& pNode) {
    this->node = pNode;
}

template<class T> void Tree<T>::addChild(Tree<T>* pChild) {
    if (this->children == nullptr) {
        this->children = new std::list<Tree<T>*>();
    }

    this->children->push_back(pChild);
}

template<class T> const Tree<T>* Tree<T>::getChild(int pIndex) {
    if (true) {
    }

    return this->children[pIndex];
}

template<class T> void Tree<T>::printTree(const Tree<T>* pTree,
        int pLevel = 0) {
    for (int i = 0; i < pLevel; i++) {
        std::cout << "  "; // Print 2 spaces for each level
    }

    std::cout << pTree->node << std::endl;

    if (pTree->children != nullptr) {
        for (typename std::list<Tree<T>*>::iterator i =
                pTree->children->begin(); i != pTree->children->end(); i++) {
            printTree(*i, pLevel + 1);
        }
    }
}

template<class T> const std::list<Tree<T>*>* Tree<T>::getChildren() {
    return this->children;
}

template<class T> bool Tree<T>::operator==(const Tree<T>& other) const {
    bool ret = false;

    if (this->node == other.node) {
        ret = true;
        typename std::list<Tree<T>*>::iterator i, j;
        for (i = this->children->begin(), j = other.children->begin();
                i != this->children->end() && j != other.children->end();
                i++, j++) {
            ret = ret && (operator==(*i, *j)); // This is the line with the error
        }
    }

    return ret;
}

int main(int argc, char **argv) {
    Tree<int> a1(1), b1(2), c1(3);
    Tree<int> a2(1), b2(2), c2(3);

    a1.addChild(&b1);
    a1.addChild(&c1);

    a2.addChild(&b2);
    a2.addChild(&c2);

    bool ret = a1 == a2;

    std::cout << ret << std::endl;
    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-09 16:00:29

当取消引用返回Tree*s时,迭代器返回Tree*s。

您正在两个Tree::operator== s上调用operator==或空闲operator==,这是没有发现的(不足为奇)。如果你想比较这些树,你需要再次取消引用。并在您使用infix ==时使用它。所以**i==**j

您应该首先检查null -- *i&&*j&&(**i==**j) --除非您可能希望两个空指针比较相等?(...)||(!*j&&!*i),或者作为一个可能的优化:(*i==*j)||(*i&&*j&&(**i==**j)),它在两个指针相同时跳过相等,并减少分支。

或者,如果您不想比较Tree,而是要比较指针,请执行*i == *j

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21661246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档