2012-06-25 141 views
-1

我創造了這個簡單的程序來學習的shared_ptr樹與升壓shared_ptr的

using namespace std; 
#define Yes 1 
#define No 0 

class Node 
{ 
public: 
boost::shared_ptr<Node> left; 
boost::shared_ptr<Node> rigth; 
int nVal; 
Node(); 
Node(int); 
~Node(); 
int getVal(void); 
void setVal(int); 

}; 

方法

Node::Node() 
{ 
cout << "creating node empty" << endl; 
nVal = 0; 
left.reset(); 
rigth.reset(); 

} 

Node::~Node() 
{ 
cout << "entering destructor" << nVal << endl; 
} 

Node::Node(int n) 
{ 
cout << "creating node with value" << n << endl; 
nVal = n; 
left.reset(); 
rigth.reset(); 
} 

int Node::getVal(void) 
{ 
cout << "returning value" << endl; 
return this->nVal; 
} 

void Node::setVal(int n) 
{ 
cout << "setting value" << endl; 
nVal = n; 
} 

class Tree 
    { 
    public: 
boost::shared_ptr<Node> root; 
    Tree(); 
    ~Tree(); 
    void findParent(int n, int &found, boost::shared_ptr<Node> &parent); 
    void add(int n); 
    void post(boost::weak_ptr<Node> q); 
    void del(int n); 

    }; 

Tree::Tree() 
{ 
root.reset(); 
} 

Tree::~Tree() 
{ 
cout << "deleting tree" << endl; 
} 

樹找到父

void Tree::findParent(int n, int& found, boost::shared_ptr<Node> &parent) 
{ 
    boost::shared_ptr<Node> q; 
    found = No; 

cout << "looking parent of" << n << endl; 
if(parent.use_count() == 0) 
{ 
    cout << "not found" << endl; 
    return; 
    } 

q=parent; 

while (!q.use_count()) 
{ 
    if(q->nVal == n) 
    { 
     cout << "found" << endl; 
     found = Yes; 
     return; 

    } 

    if (q->rigth->nVal) 
    { 
     cout << "looking to the rigth" << endl; 
     parent = q; 
     q = q->left; 
    } 
    else 
    { 
     cout << "looking to the left" << endl; 
     parent = q; 
     q = q->rigth; 
    } 
} 
} 

樹添加

void Tree::add(int n) 
{ 
int found; 

boost::shared_ptr<Node> parent; 
findParent(n, found,parent); 
if(found == Yes) 
{ 
    cout << "no such node exist" << endl; 
} 

else 
{ 

    boost::shared_ptr<Node> t(new Node(n)); 
    t->rigth.reset(); 
    t->left.reset(); 

    if (parent.get()== 0) 
    { 
     parent = t; 
    } 
    else 
    { 
     parent->nVal > n ? parent->left = t : parent->rigth = t; 
    } 
} 
} 

主營:

int THREADS_HOW_MANY = 0; 

int main() 
{ 
Tree bt; 
bt.add(10); 
bt.add(4); 
bt.add(12); 
bt.add(2); 
bt.add(8); 
bt.add(15); 
bt.add(15); 



return 0; 
} 

現在q是,爲什麼不工作,爲什麼給這個輸出:

empty constructor only the root is reset 
    looking parent of10 
    not found 
    creating node with value10 
    entering destructor10 
    looking parent of4 
    not found 
    creating node with value4 
    entering destructor4 
    looking parent of12 
    not found 
    creating node with value12 
    entering destructor12 
looking parent of2 
not found 
    creating node with value2 
entering destructor2 

固定它現在爲DIFF它似乎節點正在創建和刪除沒有添加到樹爲什麼?

+0

parent.reset(); if(parent.use_count()== 0)//這總是正確的! –

+0

改變了它:(:謝謝。仍然不正確 –

+0

q =父; while(!q.use_count())//總是假,循環不會運行 總之,你不能只是步自己的代碼在調試器中,看看那裏有什麼?! –

回答

0

這是你的代碼:

 
// from Tree::add 
boost::shared_ptr t(new Node(n)); 
boost::shared_ptr parent(new Node(n)); 
findParent(n, found,parent); 

// from tree::findParent 
boost::shared_ptr q(new Node(n)); 

因此,該程序對每個價值創造節點3倍,按要求。 shared_ptr可以確保每個節點都被正確地銷燬,因此節點析構函數也被調用3次。

+0

好的我該如何解決它(:,我該如何告訴它只指向創建的節點,而不是再次。 –

+0

nv i通過刪除新節點(n)得到它的部分我仍然不明白爲什麼它不指向兒子。 –