English 中文(简体)
为什么这个点子没有被分配到新的物体?
原标题:Why is this pointer not being assigned to a new object?
  • 时间:2012-04-14 16:39:18
  •  标签:
  • c++
  • pointers

I m 定义双双平级搜索树类:

template <class T>
class BSTNode
{
  public:
    BSTNode(){
      right = left = 0;
    }

    BSTNode(const T& el, BSTNode * l = 0, BSTNode * r = 0)
    {
    val = el; left = l; right = r;
    }   

    T val;
    BSTNode * right;
    BSTNode * left;
};

template <class T>
class BST
{
  public:
    BST(){ root = 0;}           
    void Insert(const T& el);
  private:
    BSTNode<T> * root;
};

and I implement the Insert() function like this:

template <class T>
void BST<T>::Insert(const T& el)
{
  bool bEqual = false;
  BSTNode<T> * p = root;

  while(p)
  {
    if(el == p->val)
    {
      bEqual = true;
      return;
    }
    if(el < p->val)
    {
      // go left
      p = p->left;
    }
    if(el > p->val)
    {
      // go right
      p = p->right;
    }
  }

  if(!p)
  {
    std::cout << "creating new node " << el << "
";
    p = new BSTNode<T>(el);
  }  
}

为什么<代码>root点变量停留在0,而不是新物体的地址?

最佳回答

您从来不做root = ?; 载于您的代码;还有p = 新的BSTNode<T>el); 泄漏记忆。

我的猜测是,你希望<代码>p>成为点人,这样你就可以改变原点。

 BSTNode<T> *& p = root; // watch out, it won t solve anything

But, in such case, p is not-reassignable. You may want to check if pointer you assign to p is null and just insert the new value in correct place (eg. p->left = new BSTNode<T>(el);) and reassign p only when given pointer is not null.

我指的是:

template <class T>
void BST<T>::Insert(const T& el)
{
  bool bEqual = false;
  BSTNode<T> * p = root;

  if (p == 0)
  {
     root = new BSTNode<T>(el);
     return;
  }

  while(true)
  {
    if(el == p->val)
    {
      bEqual = true;
      return;
    }
    if(el < p->val)
    {
      if (p->left == 0)
      {
        p->left = new BSTNode<T>(el);
        return;
      }
      p = p->left;
    }
    if(el > p->val)
    {
      if (p->right == 0)
      {
        p->right = new BSTNode<T>(el);
        return;
      }
      p = p->right;
    }
  }
} 
问题回答

由于在建造物体时

the statement

  p= root

最初将p定为无效。

你们制造了一个新物体,把其地址转到一页而不是根基......

这只是一个根本的复制件,而不是一个提及点。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签