gpt4 book ai didi

c++ - 没有匹配函数调用 ‘Tree::operator==(Tree*&, Tree*&) const’

转载 作者:行者123 更新时间:2023-11-28 07:06:50 28 4
gpt4 key购买 nike

我有这个通用树实现,但在编写递归树比较时遇到此错误。在第 89 行,我收到此错误:没有用于调用“Tree::operator==(Tree&, Tree&) 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;
}

最佳答案

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

您在两个 Tree* 上调用 Tree::operator== 或免费的 operator==,这就是没有找到(不足为奇)。如果要比较树,则需要再次取消引用。并在使用时使用中缀 == 。所以**i==**j

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

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

关于c++ - 没有匹配函数调用 ‘Tree<int>::operator==(Tree<int>*&, Tree<int>*&) const’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21661246/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com