爲節點實現了一個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