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

用户4833046

我尝试使用实现二叉树,std::unique_ptr但是出现错误,并且我不理解输出错误。

代码如下:

#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);
}

我认为错误是在插入函数中,并且与参数初始化有关。

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); ^

在/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:错误:初始化'void BinTree :: insert(std :: unique_ptr>,T)[with T = int]'的参数1 void BinTree :: insert(^

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>]'
if(node-> data <key)insert(node-> right,key); ^

在/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:错误:初始化'void BinTree :: insert(std :: unique_ptr>,T)[with T = int]'的参数1 void BinTree :: insert(^

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); ^

在/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:错误:初始化'void BinTree :: insert(std :: unique_ptr>,T)[with T = int]'的参数1 void BinTree :: insert(

逻辑资料

该错误是由于您尝试复制BinTree::insertfrom的参数而引起的tree.rootstd::unique_ptr仅可移动。

我的猜测是nodeinBinTree::insert应该通过引用传递。原因:

  1. 您必须将std::movetree.root插入(如果通过值传递),这将窃取所有权
  2. 您正在中进行移动分配BinTree::insert,这些更改不会通过tree.root

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C ++传递std :: unique_ptr对象作为函数参数引用的正确方法

来自分类Dev

Visual Studio 2013 C ++-将std :: unique_ptr传递给绑定函数

来自分类Dev

如何将 unique_ptr 对象作为参数传递给作为库的类

来自分类Dev

std :: unique_ptr作为函数的out参数

来自分类Dev

使用 std::move 传递的 C++ unique_ptr 右值不起作用

来自分类常见问题

如何将std :: unique_ptr传递给函数

来自分类Dev

C ++将删除的函数std :: unique_ptr与基类一起使用

来自分类Dev

错误 C2664“BOOL CryptBinaryToStringW(const BYTE *,DWORD,DWORD,LPWSTR,DWORD *)”:无法将参数 4 从“std::unique_ptr”转换为“LPWSTR”

来自分类Dev

C ++ std :: unique_ptr在std :: map中

来自分类Dev

如何使用std :: vector <unique_ptr <T >>作为默认参数

来自分类Dev

C ++“错误:将'const std :: map <int,std :: basic_string <char>>'作为...的'this'参数传递”

来自分类Dev

如何将非静态成员函数作为unique_ptr删除器传递

来自分类Dev

C ++ std :: unique_ptr从函数返回并测试null

来自分类Dev

std :: unique_ptr用于需要自由的C函数

来自分类Dev

C ++ Qt std :: unique_ptr Qt版本在哪里?

来自分类Dev

C ++ std :: unique_ptr但不可移动吗?

来自分类Dev

使用std :: move(nullptr)的unique_ptr的operator =的C ++错误

来自分类Dev

C ++ 03中std :: unique_ptr的仿真

来自分类Dev

带有 STL 容器的 C++ std::unique_ptr

来自分类Dev

使用C ++ std :: unique_ptr <>或std :: shared_ptr <>来管理Objective-C对象

来自分类Dev

使用C ++ std :: unique_ptr <>或std :: shared_ptr <>来管理Objective-C对象

来自分类Dev

参数std :: unique_ptr <T> &&的std :: move或std :: forward

来自分类Dev

错误C2248:'std :: unique_ptr <_Ty> :: unique_ptr':无法访问在类'std :: unique_ptr <_Ty>'中声明的私有成员

来自分类Dev

错误C2248:'std :: unique_ptr <_Ty> :: unique_ptr':无法访问在类'std :: unique_ptr <_Ty>'中声明的私有成员

来自分类Dev

C ++ 11将unique_ptr转换为基类

来自分类Dev

使用地图内的std :: unique_ptr作为键

来自分类Dev

将std :: unique_ptr插入std :: set

来自分类Dev

将std :: unique_ptr <Derived>转换为std :: unique_ptr <Base>

来自分类Dev

将std :: unique_ptr <Derived>转换为std :: unique_ptr <Base>

Related 相关文章

  1. 1

    C ++传递std :: unique_ptr对象作为函数参数引用的正确方法

  2. 2

    Visual Studio 2013 C ++-将std :: unique_ptr传递给绑定函数

  3. 3

    如何将 unique_ptr 对象作为参数传递给作为库的类

  4. 4

    std :: unique_ptr作为函数的out参数

  5. 5

    使用 std::move 传递的 C++ unique_ptr 右值不起作用

  6. 6

    如何将std :: unique_ptr传递给函数

  7. 7

    C ++将删除的函数std :: unique_ptr与基类一起使用

  8. 8

    错误 C2664“BOOL CryptBinaryToStringW(const BYTE *,DWORD,DWORD,LPWSTR,DWORD *)”:无法将参数 4 从“std::unique_ptr”转换为“LPWSTR”

  9. 9

    C ++ std :: unique_ptr在std :: map中

  10. 10

    如何使用std :: vector <unique_ptr <T >>作为默认参数

  11. 11

    C ++“错误:将'const std :: map <int,std :: basic_string <char>>'作为...的'this'参数传递”

  12. 12

    如何将非静态成员函数作为unique_ptr删除器传递

  13. 13

    C ++ std :: unique_ptr从函数返回并测试null

  14. 14

    std :: unique_ptr用于需要自由的C函数

  15. 15

    C ++ Qt std :: unique_ptr Qt版本在哪里?

  16. 16

    C ++ std :: unique_ptr但不可移动吗?

  17. 17

    使用std :: move(nullptr)的unique_ptr的operator =的C ++错误

  18. 18

    C ++ 03中std :: unique_ptr的仿真

  19. 19

    带有 STL 容器的 C++ std::unique_ptr

  20. 20

    使用C ++ std :: unique_ptr <>或std :: shared_ptr <>来管理Objective-C对象

  21. 21

    使用C ++ std :: unique_ptr <>或std :: shared_ptr <>来管理Objective-C对象

  22. 22

    参数std :: unique_ptr <T> &&的std :: move或std :: forward

  23. 23

    错误C2248:'std :: unique_ptr <_Ty> :: unique_ptr':无法访问在类'std :: unique_ptr <_Ty>'中声明的私有成员

  24. 24

    错误C2248:'std :: unique_ptr <_Ty> :: unique_ptr':无法访问在类'std :: unique_ptr <_Ty>'中声明的私有成员

  25. 25

    C ++ 11将unique_ptr转换为基类

  26. 26

    使用地图内的std :: unique_ptr作为键

  27. 27

    将std :: unique_ptr插入std :: set

  28. 28

    将std :: unique_ptr <Derived>转换为std :: unique_ptr <Base>

  29. 29

    将std :: unique_ptr <Derived>转换为std :: unique_ptr <Base>

热门标签

归档