2013-03-13 163 views
0

我正在使用二叉查找樹。在這裏我正在寫一個函數來從樹中刪除一個項目。在下面的代碼:複製構造函數

if(root = NULL)//if there is nothing in the tree 
{ 
    cout<<"the Tree is empty"<<endl;//ouput to the screen 
    return;//exit the function 
} 

bool isFound = false;//tells us if the item is found 
Node* tmp = new Node();//declare a temp pointer 
Node* tmp2 = new Node();;//declare a temp pointer 
tmp* = *root;//assign the pointer to something 

它調用拷貝構造函數,但我有它,現在我只是在複製這樣的價值觀:

Node& Node::operator= (const Node& node) 
{ 
    data = node.data; 
    left = node.left; 
    right = node.right; 
    return *this; 
} 
+0

有什麼實際問題? – Michael 2013-03-13 11:58:55

回答

1

你分配的指針,以分配你需要

*tmp = *root; 

tmprootNode*類型的對象; *tmp*root的類型爲Node

+0

然後我需要寫一個拷貝構造函數嗎? – compprog254 2013-03-13 11:39:15

+0

@TravisLeonSorensen是否在管理資源?是否定義了資源的所有權? (這不是一個問題,我只能用這些信息來回答) – 2013-03-13 11:39:56

+0

@TravisLeonSorensen更好閱讀 - http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – 2013-03-13 11:41:01

0
if(root = NULL) 

將其更改爲

if(root == NULL) 

這也是錯誤的:

tmp* = *root;//assign the pointer to something