Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++ 二进制搜索树析构函数:free()无效指针_C++_C++11_Binary Search Tree_Destructor - Fatal编程技术网

C++ 二进制搜索树析构函数:free()无效指针

C++ 二进制搜索树析构函数:free()无效指针,c++,c++11,binary-search-tree,destructor,C++,C++11,Binary Search Tree,Destructor,对于我当前的编程任务,我必须构造一个二叉搜索树,并使用它按字母顺序插入文件中的单词,以形成一致性列表。程序完整,输出正确;程序的主要部分工作得很好。但是当程序完成并调用析构函数时,./concordancebst:free():无效指针中出现错误,程序崩溃。我不知道问题是什么,我的析构函数看起来可以工作,在线查看代码时,它似乎也可以工作。我可能可以通过使用std::unique_ptr来解决这个问题,但从程序范围的角度来看,这似乎有点过头了(也有点过头了,因为我们没有涉及智能指针,我们在类中所做

对于我当前的编程任务,我必须构造一个二叉搜索树,并使用它按字母顺序插入文件中的单词,以形成一致性列表。程序完整,输出正确;程序的主要部分工作得很好。但是当程序完成并调用析构函数时,./concordancebst:free():无效指针中出现
错误,程序崩溃。我不知道问题是什么,我的析构函数看起来可以工作,在线查看代码时,它似乎也可以工作。我可能可以通过使用
std::unique_ptr
来解决这个问题,但从程序范围的角度来看,这似乎有点过头了(也有点过头了,因为我们没有涉及智能指针,我们在类中所做的涉及动态内存的一切都是通过手动分配/释放来处理的)

BST.h

#ifndef BST_H
#define BST_H
#include <string>

class BST {
public:
   BST() { root = nullptr; }
   ~BST();

   void insert(const std::string word);
   int getCount(const std::string word);
   int length();

   friend std::ostream& operator << (std::ostream &out_s, BST b);
private:
   struct Node {
      std::string word;
      int count;
      Node *left;
      Node *right;
      Node() { word = ""; count = 0; left = nullptr; right = nullptr;}
      ~Node();
   };
   Node *root;

   void RInsert(Node* &t, std::string word);
   int countNodes(Node *p);
   void print(Node *p, std::ostream &out_s);
};
#endif
\ifndef BST\u H
#定义BST_H
#包括
BST级{
公众:
BST(){root=nullptr;}
~BST();
无效插入(常量标准::字符串字);
int getCount(const std::string word);
int length();
friend std::ostream&操作符左)+countNodes(p->右)+1;
}
int BST::getCount(常量std::字符串字){
节点*p=根;
while(true){
如果(p==nullptr)
返回0;
else if(word.compare(p->word)<0)
p=p->左;
else if(word.compare(p->word)==0)
返回p->count;
其他的
p=p->右;
}
}
int BST::length(){
返回countNodes(root);
}
void BST::RInsert(节点*&t,标准::字符串字){
if(t==nullptr){
t=新节点;
t->word=word;
t->left=nullptr;
t->right=nullptr;
t->count++;
}
else if(word.compare(t->word)==0)
t->count++;
else if(word.compare(t->word)<0)
RInsert(t->左,单词);
else//word.compare(t->word>0)
RInsert(t->右,单词);
}
void BST::insert(常量std::字符串字){
RInsert(词根、单词);
}
void BST::print(节点*p,标准::ostream&out){
如果(p!=nullptr){
打印(p->左,外);
出局
此代码间接地违反了。
BST
BST::Node
需要复制构造函数和赋值运算符才能安全使用

编辑 最低限度的测试用例如下所示:

#include <iostream>
#include "BST.h"

int main()
{
    BST t;

    t.insert("A");

    std::cout << t << std::endl;
    return 0;
}
#包括
#包括“BST.h”
int main()
{
英国理工学院;
t、 插入(“A”);

std::cout?“main”与“complete”一样。脱离主题:为自己节省一点输入和一两个时钟周期。
Node(){word=”“;count=0;left=nullptr;right=nullptr;}
可以是
Node():word(“”),count(0),left(nullptr),right(nullptr){}
您告诉我们的错误,您提到了
free
函数失败。我在您粘贴的代码段中没有看到
free
函数调用。您确定您正在向我们显示相关代码吗?另一方面,您是否有可能在分配内存时使用
free
释放内存
new
?@AlgirdasPreidžius值得探索的观点很好,但在
delete
的黑暗深处,人们经常会发现
免费的
。MCVE有两个很好的理由:它迫使提问者更深入地调查他们的代码和问题。通常情况下,构建最小的案例会暴露错误并保存错误提出问题的两个方面。能够导出最少的代码是一项重要的调试技能,可能就在使用调试器之后。其次,MCVE允许任何人将程序代码粘贴到他们选择的IDE中,并直接看到问题所在。它消除了复制提问者使用的测试用例所涉及的猜测,并且想知道错误是否存在于未显示的代码中浪费了很多时间。哦,现在我明白了你所说的实现副本构造函数的意思。现在我明白了,为了让代码在不通过b的情况下按预期工作,实际上需要它。我的教授从未谈论过三的规则,所以我不知道,但从现在起,我将确保遵循它。谢谢太多了!没问题。还要注意五条规则(基本上是三条规则,还有额外的方法来处理
std::move
)。这里有更多好的阅读:
std::ostream &operator << (std::ostream &out_s, BST b)
                                       Pass by value^. b is copied.
std::ostream &operator << (std::ostream &out_s, BST & b)
#include <iostream>
#include "BST.h"

int main()
{
    BST t;

    t.insert("A");

    std::cout << t << std::endl;
    return 0;
}