字符串比较失败-C++
String comparison fails - C++
当我试图向二进制搜索树添加新节点时,我的程序崩溃。
崩溃消息:Access violation reading location.
xstring: return (compare(0, this->_Mysize, _Right._Myptr(), _Right.size()));
中的线路
代码:
main:
BSNode* bs = new BSNode("6");
bs->insert("2");
bs->insert("8");
崩溃的功能:
if (value.compare(_data) > 0)
{
if (this->isLeaf())
{
_right = new BSNode(value);
}
else
{
_right->insert(value);
}
}
else if (value.compare(_data) < 0)
{
if (this->isLeaf())
{
_left = new BSNode(value);
}
else
{
_left->insert(value);
}
}
它在第1行"if(value.compare(_data)>0)"处崩溃。
更多代码:
BSNode::BSNode(string value)
{
_data = value;
_right = NULL;
_left = NULL;
}
谢谢,奥马尔。
它运行良好。。。
#include "BSNode.h"
BSNode::BSNode(string value)
{
_data = value;
_right = NULL;
_left = NULL;
}
void BSNode::insert(string value)
{
if (value.compare(_data) < 0)
{
if (_left->is_empty())
{
_left = new BSNode(value);
} else {
_left->insert(value);
}
} else
if (value.compare(_data) > 0)
{
if (_right->is_empty())
{
_right = new BSNode(value);
} else {
_right->insert(value);
}
}
}
bool BSNode::is_empty()
{
return (this == NULL);
}
bool BSNode::is_leaf()
{
return (!this->is_empty() && _left->is_empty() && _right->is_empty());
}
void BSNode::print_bfs()
{
queue<BSNode*> queue;
queue.push(this);
BSNode * tmp;
while (!queue.empty())
{
tmp = queue.front();
queue.pop();
if (tmp->is_empty())
continue;
cout << tmp->_data << ", ";
queue.push(tmp->_left);
queue.push(tmp->_right);
}
cout << endl;
}
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 如何让LLDB在成功时退出,在失败时等待
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 生成MRPT库时cmake配置失败
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- variadic模板中的模板参数推导失败