为节点实现了一个C++类,但以某种方式在删除节点时仍然出现错误。C++将已删除节点的子节点移到树中的父节点
一般algorithim是这样的:
- 设定孩子的PARENT_到PARENT_
- 孩子添加到父
- 的孩子从这个节点 的孩子取出孩子
这里是我的类文件:
template <typename E>
class TNode{
...
bool remove(){
if ((parent_==NULL) && (children_.size()>0)){
cout << "can't remove the root if it has children\n";
return false;
}
else{
parent_->children_.erase(std::remove(parent_->children_.begin(), parent_->children_.end(), this), parent_->children_.end());
for (int i=children_.size()-1;i>=0; i--){
//my error is happening here
parent_ = parent_ -> children_;
this = children_ -> this;
parent_ = parent_ -> this;
}
//delete (deallocate memory of) this node
delete this;
return true;
}
}
friend class gTree<E>;
private:
E data_;
gTNode<E>* parent_;
std::vector<gTNode<E>*> children_;
};
有人能指引我正确的方向吗?
编辑的错误是这样的:
gTree.h: In member function ‘bool gTNode<E>::remove()’:
gTree.h:50:29: error: expected unqualified-id before ‘this’
this = children_ -> this;
^
gTree.h:50:29: error: expected ‘;’ before ‘this’
gTree.h:51:30: error: expected unqualified-id before ‘this’
parent_ = parent_ -> this;
^
gTree.h:51:30: error: expected ‘;’ before ‘this’
见上 – nickoba