简体   繁体   English

C ++将std :: unique_ptr作为参数传递

[英]C++ passing std::unique_ptr as an argument

I tried to implement binary tree using std::unique_ptr but errors come up and I don't understand the output error. 我尝试使用std::unique_ptr实现二叉树,但是出现错误,并且我不理解输出错误。

The code is the following: 代码如下:

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

template <typename T>
class BinTreeNode {
public: 
    BinTreeNode(T key): data {key}, left {nullptr}, right {nullptr} {}
    ~BinTreeNode() {}
    T data;
    std::unique_ptr<BinTreeNode<T>> left;
    std::unique_ptr<BinTreeNode<T>> right;
};

template <typename T>
class BinTree {
public:
    BinTree() : root {nullptr} {} 
    ~BinTree() {}
    std::unique_ptr<BinTreeNode<T>> root;

    void insert(std::unique_ptr<BinTreeNode<T>> node, T key);
};

template <typename T>
void BinTree<T>::insert(
    std::unique_ptr<BinTreeNode<T>> node, 
    T key)
{
    if(node){ // != nullptr
        if(node->data < key) insert(node->right, key);
        else insert(node->left, key);
    } 
    else{
        std::unique_ptr<BinTreeNode<T>> u_ptr(new BinTreeNode<T>(key));
        node = std::move(u_ptr);
    }
}

int main(){
    BinTree<int> tree();
    tree.insert(tree.root, 10);
}

I assume the error is in insert function and is related to argument initialisation. 我认为错误是在插入函数中,并且与参数初始化有关。

BinTree.cpp:65:27: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinTreeNode; BinTree.cpp:65:27:错误:使用删除的函数'std :: unique_ptr <_Tp,_Dp> :: unique_ptr(const std :: unique_ptr <_Tp,_Dp>&)[with _Tp = BinTreeNode; _Dp = std::default_delete >]' tree.insert(tree.root, 10); _Dp = std :: default_delete>]'tree.insert(tree.root,10); ^ ^

In file included from /usr/include/c++/4.9/memory:81:0, from BinTree.cpp:2: /usr/include/c++/4.9/bits/unique_ptr.h:356:7: note: declared here unique_ptr(const unique_ptr&) = delete; 在/usr/include/c++/4.9/memory:81:0中包含的文件中,从BinTree.cpp:2:/usr/include/c++/4.9/bits/unique_ptr.h:356:7:注意:在此处声明unique_ptr (const unique_ptr&)=删除; ^ ^

BinTree.cpp:35:6: error: initializing argument 1 of 'void BinTree::insert(std::unique_ptr >, T) [with T = int]' void BinTree::insert( ^ BinTree.cpp:35:6:错误:初始化'void BinTree :: insert(std :: unique_ptr>,T)[with T = int]'的参数1 void BinTree :: insert(^

BinTree.cpp: In instantiation of 'void BinTree::insert(std::unique_ptr >, T) [with T = int]': BinTree.cpp:65:27: required from here BinTree.cpp:40:47: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinTreeNode; BinTree.cpp:在'void BinTree :: insert(std :: unique_ptr>,T)[with T = int]'的实例中:BinTree.cpp:65:27:从此处需要BinTree.cpp:40:47:错误:使用删除的函数'std :: unique_ptr <_Tp,_Dp> :: unique_ptr(const std :: unique_ptr <_Tp,_Dp>&)[with _Tp = BinTreeNode; _Dp = std::default_delete >]' _Dp = std :: default_delete>]'
if(node->data < key) insert(node->right, key); if(node-> data <键)insert(node->右键,键); ^ ^

In file included from /usr/include/c++/4.9/memory:81:0, from BinTree.cpp:2: /usr/include/c++/4.9/bits/unique_ptr.h:356:7: note: declared here unique_ptr(const unique_ptr&) = delete; 在/usr/include/c++/4.9/memory:81:0中包含的文件中,从BinTree.cpp:2:/usr/include/c++/4.9/bits/unique_ptr.h:356:7:注意:在此处声明unique_ptr (const unique_ptr&)=删除; ^ ^

BinTree.cpp:35:6: error: initializing argument 1 of 'void BinTree::insert(std::unique_ptr >, T) [with T = int]' void BinTree::insert( ^ BinTree.cpp:35:6:错误:初始化'void BinTree :: insert(std :: unique_ptr>,T)[with T = int]'的参数1 void BinTree :: insert(^

BinTree.cpp:41:30: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = BinTreeNode; BinTree.cpp:41:30:错误:使用删除的函数'std :: unique_ptr <_Tp,_Dp> :: unique_ptr(const std :: unique_ptr <_Tp,_Dp>&)[with _Tp = BinTreeNode; _Dp = std::default_delete >]' else insert(node->left, key); _Dp = std :: default_delete>]'else insert(node-> left,key); ^ ^

In file included from /usr/include/c++/4.9/memory:81:0, from BinTree.cpp:2: /usr/include/c++/4.9/bits/unique_ptr.h:356:7: note: declared here unique_ptr(const unique_ptr&) = delete; 在/usr/include/c++/4.9/memory:81:0中包含的文件中,从BinTree.cpp:2:/usr/include/c++/4.9/bits/unique_ptr.h:356:7:注意:在此处声明unique_ptr (const unique_ptr&)=删除; ^ ^

BinTree.cpp:35:6: error: initializing argument 1 of 'void BinTree::insert(std::unique_ptr >, T) [with T = int]' void BinTree::insert( BinTree.cpp:35:6:错误:初始化'void BinTree :: insert(std :: unique_ptr>,T)[with T = int]'的参数1 void BinTree :: insert(

The error is caused by you trying to copy-construct the argument of BinTree::insert from tree.root . 该错误是由您尝试从tree.root复制构造BinTree::insert的参数tree.root std::unique_ptr is move-only. std::unique_ptr仅可移动。

My guess is that node in BinTree::insert should be passed by reference. 我的猜测是BinTree::insert中的node应该通过引用传递。 Reasons: 原因:

  1. You'd have to std::move the tree.root into it (if passed by value), which would steal the ownership 您必须std::move tree.root std::move到其中(如果通过值传递),这将窃取所有权
  2. You're move-assigning to it in BinTree::insert , these changes are not made to pass tree.root 您正在BinTree::insertBinTree::insert移动分配,而不会通过tree.root进行这些更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM