C++ 为什么是C++;产生分段错误的代码?

C++ 为什么是C++;产生分段错误的代码?,c++,C++,我特别提到的代码函数是getCount()。还有几个我没有在这里包括的函数(比如查找二叉树的高度和总节点数),它们工作正常,结果正确。另一方面,getCount()生成除第一个节点(树的顶部第一个节点)之外的分段错误。有什么想法吗 #include <string> #include <algorithm> #include <iostream> class Word { public: std::string keyval;

我特别提到的代码函数是getCount()。还有几个我没有在这里包括的函数(比如查找二叉树的高度和总节点数),它们工作正常,结果正确。另一方面,getCount()生成除第一个节点(树的顶部第一个节点)之外的分段错误。有什么想法吗

#include <string> 
#include <algorithm>
#include <iostream>

class Word {
    public:
        std::string keyval;
        long long count;
        Word() {
            keyval = "";
            count = 0;
        }
        Word(std::string S) {
            keyval = S;
            count = 1;
        }
};

class WordBST {
    public:
        Word node;
        WordBST* left_child;
        WordBST* right_child;
        WordBST(std::string key);        
        void add(std::string key){
            if (key == node.keyval){
                node.count++;
            }
            else if (key < node.keyval){
                if (left_child == NULL){
                    left_child = new WordBST(key);
                }else {
                    left_child->add(key);
                }
            }else {
                if (right_child == NULL){
                    right_child = new WordBST(key);
                }else {
                    right_child->add(key);
                }
            }
        }
        long long getCount(std::string key){
            if (key == node.keyval){
                return (node.count);
            }
            else if (key < node.keyval){
                left_child->getCount(key);
            }else if(key > node.keyval){
                right_child->getCount(key);
            }else return 0;
            /*else {
                if (key < node.keyval){
                    left_child->getCount(key);
                }else{
                    right_child->getCount(key);
                }
            }*/
        }
};

WordBST::WordBST(std::string key) {
    node = Word(key);
    left_child = NULL;
    right_child = NULL;
}
#包括
#包括
#包括
类词{
公众:
std::字符串keyval;
长计数;
单词(){
keyval=“”;
计数=0;
}
单词(标准::字符串S){
keyval=S;
计数=1;
}
};
类字{
公众:
词节点;
WordBST*左_子项;
WordBST*右\子;
WordBST(标准::字符串键);
无效添加(标准::字符串键){
if(key==node.keyval){
node.count++;
}
else if(key添加(键);
}
}否则{
if(right_child==NULL){
右\u子项=新单词BST(键);
}否则{
右键\u子项->添加(键);
}
}
}
long getCount(标准::字符串键){
if(key==node.keyval){
返回(node.count);
}
else if(key获取计数(键);
}else if(key>node.keyval){
右键\u child->getCount(键);
}否则返回0;
/*否则{
if(键获取计数(键);
}否则{
右键\u child->getCount(键);
}
}*/
}
};
WordBST::WordBST(标准::字符串键){
节点=字(键);
left_child=NULL;
右_child=NULL;
}

这是因为您让代码在没有返回语句的情况下结束运行

long long getCount(std::string key){
    if (key == node.keyval){
        return (node.count);
    } else if (left_child && key < node.keyval){
        return left_child->getCount(key); // Added return
    } else if(right_child && key > node.keyval){
        return right_child->getCount(key); // Added return
    }
    return 0;
}
long getCount(标准::字符串键){
if(key==node.keyval){
返回(node.count);
}else if(left_child&&keygetCount(key);//添加了return
}else if(右\u子项(&key>node.keyval){
return right_child->getCount(key);//添加了return
}
返回0;
}

您还需要在整个代码中的多个位置添加空检查。您的
add
方法拥有它们,但是您的
getCount
没有。在调用它们的方法之前,您不需要检查节点的子节点是否存在。

我认为您应该这样编写getCount()

    long long getCount(std::string key){
    if (key == node.keyval){
        return (node.count);
    }
    else if (key < node.keyval && left_child != NULL){
        return left_child->getCount(key);
    }else if(key > node.keyval && right_child != NULL){
        return right_child->getCount(key);
    }else return 0;
}
long getCount(标准::字符串键){
if(key==node.keyval){
返回(node.count);
}
else if(key获取计数(键);
}else if(key>node.keyval&&right\u child!=NULL){
返回右\u子项->获取计数(键);
}否则返回0;
}

<代码>您是否尝试使用调试器?当您第一次引用这些代码时,<代码> LeTytSuth< < /C> >和<代码> RealIsHeld< /Cord>有效指针?“<代码> GETCOUND> <代码>不返回值与分割错误有关吗?”根据C++规范,Axtheta提出,不从声明为返回值的函数返回值是未定义的行为。@dasblinkenlight Short and sweet solution。这真是妙不可言。非常感谢。我想知道,没有返回声明,到底发生了什么?哦,是的,你说得对,我现在就实现空检查。@Aesthete add()函数是空的,因此不需要返回。另一方面,getCount()必须返回long。我应该马上注意到的。编辑:我看到dasblinkenlight在我回答之前就回答了。@Nico-我意识到用返回类型声明的函数应该返回一个值,我只是不相信它会导致分段错误。但正如dasblinkenlight所提到的,未定义的行为就是未定义的行为。