C++ 如何从唯一指针递归使用原始指针?

C++ 如何从唯一指针递归使用原始指针?,c++,c++11,unique-ptr,raw-pointer,C++,C++11,Unique Ptr,Raw Pointer,我正在尝试创建一个具有唯一指针的AVL树。但是我停留在递归插入节点的基本部分。此代码在调用insertNode\uuz函数时创建分段错误。我原以为仅仅传递原始指针就行了,但事实并非如此 #ifndef AVL_H #define AVL_H #include <memory> #include <iostream> template<class T> class AVL { public: template<class K,class

我正在尝试创建一个具有唯一指针的AVL树。但是我停留在递归插入节点的基本部分。此代码在调用
insertNode\uuz
函数时创建分段错误。我原以为仅仅传递原始指针就行了,但事实并非如此

#ifndef AVL_H
#define AVL_H
#include <memory>
#include <iostream>



template<class T>
class AVL
{
  public:
    template<class K,class V>
    struct nodeAVL;
    typedef std::unique_ptr<nodeAVL<int,T>> node_ptr;
    typedef nodeAVL<int,T> node;

  /* Node struct */
    template<class K,class V>
    struct nodeAVL
    {
      nodeAVL(const K& key, const V& value):
           key_ (key), value_ (value)
          { left = nullptr;right=nullptr;parent=nullptr; }
      std::unique_ptr< nodeAVL<K,V> > left, right;
          node*  parent;
      V value()
           { return value_; }
      K key()
           { return key_; }
      K key(const K& key)
           { key_ = key; }
      private:
        K key_;
        V value_;
    };
 /* end of Node struct  */


    AVL()
        { head_=nullptr; };
    void insert (const T& value)
        { std::cout<<value<<" inserting\n";
          insertNode_ (head_, head_->parent,value); }
    std::string print() 
        { std::cout<<"print\n";
          return print_inOrder_(head_, ""); }

  private:
    node_ptr head_;

    template <class N, class... Args>  //allows make_unique in c11
    std::unique_ptr<N> make_unique(Args&&... args) {
    return std::unique_ptr<N>(new N(std::forward<Args>(args)...));
}


 /* recursive insertion   */
    void insertNode_( node_ptr& current,node* parent, const T& value)
    {
      std::cout<<"segmentation fault happens here, this line doesnt print\n";
      if (current == nullptr){
        current = std::move(make_unique<node>(-1,value));
        std::cout<<current->value()<<" inserted\n";
        if (parent!= nullptr)
          std::cout<<"another issue happens here after root insertion\n";
      }
      else {
        if ( current->value() > value ) 
          insertNode_(current->left, current.get(),value);
        else if ( current->value() < value )
          insertNode_(current->right, current.get(),value);
      }
    }



 /* recursive inOrder print  */
    std::string print_inOrder_(const node_ptr& current,std::string print)
    {
      if (current != nullptr) {
      std::cout<<"<"<<current->value()<<">"<<std::endl;
        print+= "[" + std::to_string(current->value())+"]";
        print = print_inOrder_(current->left,print);
        print = print_inOrder_(current->right,print);
      }
      return print;
    }

};

#endif
\ifndef AVL\u H
#定义AVL_H
#包括
#包括
模板
类AVL
{
公众:
模板
结构节点;
typedef std::unique_ptr node_ptr;
typedef nodeAVL节点;
/*节点结构*/
模板
结构节点
{
节点AVL(常数K和键、常数V和值):
键(键),值(值)
{left=nullptr;right=nullptr;parent=nullptr;}
std::unique_ptr左、右;
节点*父节点;
V值()
{返回值\;}
K键()
{返回键}
K键(常数K和键)
{key\=key;}
私人:
K键;
V值;
};
/*节点结构的结尾*/
AVL()
{head_uz=nullptr;};
无效插入(常数T和值)

{std::coutIt必须是有错误的调用者;最好的猜测是:
insertNode_u(head_u,head_u->parent,value)
head为空。在这一行上放置一个断点并检查值。我真是太感谢你了!这解决了我的问题。我将
head->parent
更改为
nullptr
,现在它可以工作了。它必须是有故障的调用方;最好的猜测是:
insertNode_uu(head_u,head_uu->parent,value)
head为空。在这行上放置一个断点并检查值。我真是太感谢你了!这解决了我的问题。我将
head->parent
更改为
nullptr
,现在它可以工作了。