错误 C2106:'=':左操作数必须是 l 值 C++二叉树
error C2106: '=' : left operand must be l-value C++ Binary Tree
我正在写一个二进制树,开始出现错误,所以我已经删除了所有的模板,但它仍然无法编译,因为我的最终错误!写一个递归添加函数,但不确定当它发现一个空节点时如何将我的新dataClass添加到树中
#pragma once
#include <cstring>
typedef dataClass T;
//template <class T>
class treeNode
{
private:
treeNode* _greaterNode;
treeNode* _lessNode;
T _data;
public:
treeNode(T data);
void add(T data);
void del(T data);
};
//template <class T>
treeNode/*<T>*/::treeNode(T data)
{
_data = data;
_greaterNode = _lessNode = NULL;
}
//template <class T>
void treeNode/*<T>*/::add(T data)
{
if(_data == NULL)
{
// add here
this = new treeNode(data);
}
else if(data > _data)
{
// data is bigger go to greater
this->_greaterNode->add(data);
}
else if(data < _data)
{
// data is lower go to less than
this->_lessNode->add(data);
}
else
{
// data the same, throw exception
}
}
它正在突破:
if(_data == NULL)
{
// add here
this = new treeNode(data);
}
不能分配给this
!您可以像在*this = treenode(data);
中一样,对*this
进行assing,但这可能会导致其他错误,因为节点指针会被覆盖。
为什么不简单地将_data
设置为参数data
呢?
此外,在进行递归调用时,如果链接不存在,则应创建这些链接:
else if(data > _data)
{
// data is bigger go to greater
if (this->_greaterNode == NULL)
this->_greaterNode = new treeNode(data);
else
this->_greaterNode->add(data);
}
相关文章:
- 从父数组测试用例构造二叉树失败
- 打印时有二叉树问题.用户输入不打印任何内容
- 试图找到二叉树的深度
- 二叉树结构平衡,使用递归时EXC_BAD_ACCESS
- 指向二叉树中新节点的指针
- 我试图用这段代码找到二叉树的高度,但它一直返回 0,有人可以告诉我为什么吗?
- C++:如何计算二叉树中其值模块高度小于 2 的节点数?
- 二叉树级别顺序遍历在leetcode中
- 运行无限循环的最小二叉树问题
- 打印二叉树中的常见元素
- 二叉树基准测试结果
- 带有矢量重复值的二叉树打印出来
- 签入二叉树的函数是平衡C++
- 我有两棵二叉树.我想在不更改输入树的情况下深度复制两个二叉树的结果
- 如何计算给定数字在二叉树中出现的次数?
- 为什么我的二叉树会覆盖其根的叶子?
- 将二叉树的节点插入链表 (C++)
- 二叉树级别顺序插入 c++
- 在 C++ 中创建两个不同的二叉树时出现分段错误
- 如何在使用 ItemType 的模板类时将新节点插入二叉树 c++.(我是 c++ 的新手)