错误 C2106:'=':左操作数必须是 l 值 C++二叉树

error C2106: '=' : left operand must be l-value C++ Binary Tree

本文关键字:C++ 二叉树 C2106 错误 操作数      更新时间:2023-10-16

我正在写一个二进制树,开始出现错误,所以我已经删除了所有的模板,但它仍然无法编译,因为我的最终错误!写一个递归添加函数,但不确定当它发现一个空节点时如何将我的新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);
}