没有用于调用'Tree <int> :: operator ==(Tree <int> *&amp;,Tree <int> *&amp;)const'</int> </int> </int>的匹配函数

时间:2014-02-09 15:52:19

标签: c++ tree

我有这个通用树实现,我在编写递归树比较时遇到此错误。 在第89行,我收到此错误: 没有用于调用'Tree :: operator ==(Tree &amp;,Tree &amp;)const'的匹配函数 这是我的代码:

#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;
}

1 个答案:

答案 0 :(得分:1)

取消引用后Tree*的迭代器返回Tree*

您在两个Tree::operator==上呼叫operator==或免费Tree*,但未找到(不出意外)。如果要比较树,则需要再次取消引用。并且在使用时使用中缀==。所以**i==**j

你应该首先检查null - *i&&*j&&(**i==**j) - 除了你想要两个空指针比较相等吗? (...)||(!*j&&!*i)或者作为一种可能的优化:(*i==*j)||(*i&&*j&&(**i==**j))当两个指针相同时也跳过相等,并减少分支。

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