【问题标题】:Tree with boost shared_ptr带有 boost shared_ptr 的树
【发布时间】:2012-06-25 12:03:43
【问题描述】:

我创建了这个简单的程序来学习 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 似乎节点只是被创建和删除而不是添加到树为什么??

【问题讨论】:

  • parent.reset(); if(parent.use_count() == 0) // 这总是正确的!
  • 改变了它(:,谢谢。仍然不正确
  • q=父母; while (!q.use_count()) // 总是假的,循环不会运行 简而言之,你不能在调试器中单步执行你自己的代码,看看那里发生了什么吗?!

标签: c++ boost shared-ptr


【解决方案1】:

这是你的代码:

// 从树::添加 boost::shared_ptr t(新节点(n)); boost::shared_ptr parent(新节点(n)); 找到父母(n,找到,父母); // 从树::findParent boost::shared_ptr q(新节点(n));

因此,程序根据要求为每个值创建节点 3 次。 shared_ptr 确保每个节点都被正确销毁,因此节点析构函数也被调用了 3 次。

【讨论】:

  • 好的,我该如何修复它(:,我如何告诉它只指向创建的节点而不再做。
  • nv 我通过删除新的节点(n)部分得到它我仍然不明白为什么它不指向儿子。
猜你喜欢
  • 2010-10-12
  • 2010-10-11
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 2011-04-19
相关资源
最近更新 更多