2012-02-04 58 views
-1

我正在写一个二叉树,开始收到错误,所以我已经删除了所有的模板,但它仍然不会编译,难倒我最后的错误! 写一个递归的附加功能,但不知道如何到我的新的数据类添加到我的树时,发现一个空的节点错误C2106:'=':左操作数必须是l值C++二进制树

#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); 
} 
+3

'this'是'treeNode的* const'不能修改。你应该只是做'_data = data'。 – 2012-02-04 19:24:07

+0

In。书房。塔季翁。 – 2012-02-04 19:28:06

+2

而“它破”不是错误描述。 – 2012-02-04 19:28:42

回答

0

您不能分配到this!您可以剔除*this,如*this = treenode(data);,但这可能会导致其他错误,因为节点指针会被覆盖。

为什么不简单地将_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); 
} 
相关问题