gpt4 book ai didi

c++ - 使用已删除函数 std::unique_ptr

转载 作者:行者123 更新时间:2023-11-30 03:48:34 26 4
gpt4 key购买 nike

错误似乎出在函数 insert 和 printTree 中。我知道这个错误是由 unique_ptr 不可复制引起的。但我认为提供 move 和复制语义应该可以帮助我解决它。 我的问题

  1. 复制和 move 构造函数是否正确?

  2. 如果没有。我应该如何重新设计代码。请列出基本错误以及如何改正它们。

  3. 如何将 Node* parent 合并到类中?

  4. 在这种情况下,一些关于良好代码实践的提示会有所帮助

    // This is implementation of binary search tree.

    #ifndef BinarySearchTree_H

    #define BinarySearchTree_H


    #include <cstdio>
    #include <functional>
    #include <utility>
    #include <vector>
    #include <iostream>
    #include <memory>

    //template declaration
    template <class ValueType>

    class BinarySearchTree
    {

    struct Node
    {
    ValueType value;

    std::unique_ptr<Node> left;
    std::unique_ptr<Node> right;

    //Node *parent=nullptr; // How can I use parent in the class ?

    Node(){}

    //Node(const ValueType& value,std::unique_ptr<Node> left,std::unique_ptr<Node> right):value(value),left(left),right(right){}

    Node (const ValueType& value):value(value),left(nullptr),right(nullptr){}

    };

    std::unique_ptr<Node> root;

    void insert(const ValueType& value, std::unique_ptr<Node> node)
    {
    if(value< node->value)
    {
    if(node->left)
    {
    insert(value,node->left);
    }

    else
    {
    std::unique_ptr<Node> left=std::unique_ptr<Node>(new Node(value));
    node->left=left;
    }

    }

    else
    {
    if(node->right)
    {
    insert(value,node->right);
    }

    else
    {
    std::unique_ptr<Node> right=std::unique_ptr<Node>(new Node(value));
    node->right=right;
    //right->parent=node;
    }
    }
    }

    void printNode(std::unique_ptr<Node> node)
    {
    if(!node)
    {
    std::cout<<"No element in the tree\n";
    }
    else
    {
    if(node->left)
    {
    std::cout<<node->left->value<<" ";
    }
    std::cout<<node->value<<" ";
    if(node->right)
    {
    std::cout<<node->right->value<<" ";
    }
    }
    }

    public:

    BinarySearchTree():root(nullptr){}
    ~BinarySearchTree(){}

    BinarySearchTree( BinarySearchTree && rhs):root(std::move(rhs.root)){}

    BinarySearchTree& operator=(BinarySearchTree && rhs )
    {
    root=std::move(rhs.root);
    return *this;
    }

    BinarySearchTree& operator=(const BinarySearchTree & rhs )
    {
    if(this!=&rhs)
    root.reset(rhs.root);
    return *this;
    }

    void insert(const ValueType& value)
    {
    if(root==nullptr)
    {
    root=std::unique_ptr<Node>(new Node(value));

    }

    else
    {
    insert(value,root);
    }
    }
    // void remove(const ValueTypr& value);

    void printTree(const BinarySearchTree& tree)
    {
    if(tree.root)
    {
    if(tree.root->left)
    {
    printNode(tree.root->left);
    }
    printNode(tree.root);
    if(tree.root->right)
    {
    printNode(tree.root->right);
    }
    }

    else
    {
    std::cout<<"tree is empty\n";
    return;
    }
    }

    };



    #endif // BinarySearchTree

最佳答案

  1. 没有。您不能复制唯一指针。您必须决定树的深层拷贝是否有意义。
  2. 使用 move 构造函数和 move 赋值运算符代替复制构造函数和复制赋值运算符。
  3. 添加一个原始指针Node* parent。 parent 拥有自己的 child ,反之亦然。
  4. 使用std::make_unique()。避免包含不需要的 header 。 printTree()this 不起作用有什么原因吗?通常,您可以使用稍微更具可读性的语法(缩进、空行等)

关于c++ - 使用已删除函数 std::unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33119574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com