Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 返回2个不相等的字符串,其中包含==上的真值以及a.compare(b)_C++_String_Class_Binary Search Tree_Equals - Fatal编程技术网

C++ 返回2个不相等的字符串,其中包含==上的真值以及a.compare(b)

C++ 返回2个不相等的字符串,其中包含==上的真值以及a.compare(b),c++,string,class,binary-search-tree,equals,C++,String,Class,Binary Search Tree,Equals,我正在制作一个BST&AVL树,处理字符串键。 大体上,我没有添加“600”键,但在查找时显示“600”已找到 我尝试使用gdb进行调试&发现在两个不相等的字符串上==给出了一个通过if子句的真值 我还尝试使用string类的compare()函数,仍然存在相同的问题。 请帮忙 这是我的findNode函数: template<class NodeType> NodeType* Tree<NodeType>::findNode(string key, NodeType*

我正在制作一个BST&AVL树,处理字符串键。 大体上,我没有添加“600”键,但在查找时显示“600”已找到

我尝试使用gdb进行调试&发现在两个不相等的字符串上==给出了一个通过if子句的真值

我还尝试使用string类的compare()函数,仍然存在相同的问题。 请帮忙

这是我的
findNode
函数:

template<class NodeType>
NodeType* Tree<NodeType>::findNode(string key, NodeType* node)
{
    if ( node == NULL )
        return NULL;
    else if ( node->Key() == key )
        return node;
    else if ( key < node->Key() )
        findNode(key, node->Left());
    else if ( key > node->Key() )
        findNode(key, node->Right());
    else
        return NULL;
}
模板
NodeType*树::findNode(字符串键,NodeType*节点)
{
if(node==NULL)
返回NULL;
else if(节点->键()==键)
返回节点;
else if(键<节点->键())
findNode(键,节点->左());
否则如果(键>节点->键())
findNode(键,节点->右());
其他的
返回NULL;
}
这里是所有代码的链接
findNode
方法中,对
findNode(…左…
findNode(…右…
的递归调用缺少
return

模板
NodeType*树::findNode(字符串键,NodeType*节点)
{
if(node==NULL)
返回NULL;
else if(节点->键()==键)
返回节点;
else if(键<节点->键())
返回findNode(键,节点->左());
否则如果(键>节点->键())
返回findNode(key,node->Right());
其他的
返回NULL;
}
在这些情况下(大多数情况下),您的实现返回了垃圾


如果您使用
-Wall
标志编译,那么编译器将为您找到此错误


顺便说一句,此标志还发现了多个错误,您可能想检查它。

请在此处发布错误。没有人希望通过数百行代码查找错误。不要发布代码链接,而是将代码的相关部分复制粘贴到实际问题正文中。或者更好的办法是,创建一个。
template<class NodeType>
NodeType* Tree<NodeType>::findNode(string key, NodeType* node)
{
    if ( node == NULL )
        return NULL;
    else if ( node->Key() == key )
        return node;
    else if ( key < node->Key() )
        return findNode(key, node->Left());
    else if ( key > node->Key() )
        return findNode(key, node->Right());
    else
        return NULL;
}