【发布时间】:2017-04-09 02:40:02
【问题描述】:
我制作了一个二叉树类,其中包含:
int 值,BinaryTree* left,BinaryTree* right。
class BinaryTree {
private:
int value;
BinaryTree* left;
BinaryTree* right;
bool isVisited;
public:
BinaryTree();
BinaryTree createComplete(int n);
~BinaryTree();
}
我的析构函数是:
BinaryTree::~BinaryTree() {
delete left;
delete right;
}
在 clion 中运行时它运行良好,但在我的终端中我得到 段错误(核心转储)。我到处都看到人们声称这应该是析构函数。任何详细说明都会有所帮助!
我不是 stackoverflow 专家,我更新了我的 ~BinaryTree 函数,但仍然出现段错误:
BinaryTree::~BinaryTree() {
if (right != NULL) {
delete right;
}
if (left != NULL) {
delete left;
}
}
【问题讨论】:
-
能否也显示构造函数定义?
-
如何构建 BST?您能否发布足够的代码以便我们最终重现此问题?
标签: c++ recursion segmentation-fault binary-tree destructor