C ++ 11使用unique_ptr和自定义删除器

Ashwinaj

我正在尝试通过做一个简单的链表程序来学习C ++ 11 unique_ptr的用法。对于我一生,我无法弄清楚为什么使用自定义删除程序时会出现编译错误。

#include <cstdio>
#include <limits>
#include <memory>
#include <cstdlib>
#include <iostream>

using namespace std;

struct node {
    int value;
    struct node* next;
};

typedef struct node Node;

std::unique_ptr<Node> createList()
{

    std::unique_ptr<Node> head(new Node);
    Node* temp=head.get();
    temp->value=0;
    for(int i=1;i<8;i++) {
        if(temp->next==nullptr) {
            temp->next=new Node();
            temp=temp->next;
            temp->value=i;
            temp->next=nullptr;
        }
    //temp=temp->next;
    }
    return head;
}

int main()
{
    auto del1 = [](Node* p) { while(p) {std::cout << "Deleting value is : " << p->value;struct node* n=p->next;delete p; p=n;} return; };

    std::unique_ptr< Node, decltype(del1) > head(std::move(createList()),del1);
}

这是编译错误

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                   
main.cpp: In function 'int main()':                                                                                                                    
main.cpp:38:82: error: no matching function for call to 'std::unique_ptr<node, main()::<lambda(Node*)> >::unique_ptr(std::remove_reference<std::unique_
ptr<node> >::type, main()::<lambda(Node*)>&)'                                                                                                          
         std::unique_ptr< Node, decltype(del1) > head(std::move(createList()),del1);                                                                   
                                                                                  ^                                                                    
In file included from /usr/include/c++/5.3.1/memory:81:0,                                                                                              
                 from main.cpp:3:                                                                                                                      
/usr/include/c++/5.3.1/bits/unique_ptr.h:228:2: note: candidate: template<class _Up, class> std::unique_ptr<_Tp, _Dp>::unique_ptr(std::auto_ptr<_Up>&&)
  unique_ptr(auto_ptr<_Up>&& __u) noexcept;                                                                                                            
  ^                                                                                                                                                    
/usr/include/c++/5.3.1/bits/unique_ptr.h:228:2: note:   template argument deduction/substitution failed:                                               
main.cpp:38:82: note:   'std::remove_reference<std::unique_ptr<node> >::type {aka std::unique_ptr<node>}' is not derived from 'std::auto_ptr<_Up>'     
         std::unique_ptr< Node, decltype(del1) > head(std::move(createList()),del1);                                                                   
                                                                                  ^                                                                    
In file included from /usr/include/c++/5.3.1/memory:81:0,                                                                                              
                 from main.cpp:3:                                                                                                                      
/usr/include/c++/5.3.1/bits/unique_ptr.h:220:2: note: candidate: template<class _Up, class _Ep, class> std::unique_ptr<_Tp, _Dp>::unique_ptr(std::uniqu
e_ptr<_Up, _Ep>&&)                                                                                                                                     
  unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept                                                                                                      
  ^                                                                                                                                                    
/usr/include/c++/5.3.1/bits/unique_ptr.h:220:2: note:   template argument deduction/substitution failed:                                               
main.cpp:38:82: note:   candidate expects 1 argument, 2 provided                                                                                       
         std::unique_ptr< Node, decltype(del1) > head(std::move(createList()),del1);    

有任何想法吗?

天顶

您应该从返回正确的类型createList

#include <cstdio>
#include <limits>
#include <memory>
#include <cstdlib>
#include <iostream>

using namespace std;

struct node {
    int value;
    struct node* next;
};

typedef struct node Node;

auto createList()
{
    auto del1 = [](Node* p) { while(p) {std::cout << "Deleting value is : " << p->value;struct node* n=p->next;delete p; p=n;} return; };
    std::unique_ptr< Node, decltype(del1) > head(new Node,del1);

    Node* temp=head.get();
    temp->value=0;
    for(int i=1;i<8;i++) {
        if(temp->next==nullptr) {
            temp->next=new Node();
            temp=temp->next;
            temp->value=i;
            temp->next=nullptr;
        }
    //temp=temp->next;
    }
    return head;
}

int main()
{
    auto node = createList();
 }

否则,在问题所示的代码中,您应该拥有内部数据的所有权并将其移动,它们是不同类型的指针:

int main()
{
    auto del1 = [](Node* p) { while(p) {std::cout << "Deleting value is : " << p->value;struct node* n=p->next;delete p; p=n;} return; };

    std::unique_ptr< Node, decltype(del1) > head(createList().release(),del1);
}

请注意对的调用.release()
有关更多详细信息,请参见此处

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

用unique_ptr和自定义删除器包装C代码

来自分类Dev

智能指针(unique_ptr)自定义删除器错误C2027和C2338

来自分类Dev

混淆使用unique_ptr和自定义删除器

来自分类Dev

unique_ptr 的有状态自定义删除器

来自分类Dev

unique_ptr,自定义删除器和零规则

来自分类Dev

std :: unique_ptr <T []>和自定义分配器删除器

来自分类Dev

std :: unique_ptr,自定义删除器和类型更改

来自分类Dev

C ++ 11 unique_ptr数组和构造函数参数

来自分类Dev

C ++零规则:多态删除和unique_ptr行为

来自分类Dev

使用带有unique_ptr的自定义删除器

来自分类Dev

使用std :: function对象将自定义删除器传递给std :: unique_ptr

来自分类Dev

std :: unique_ptr使用带有很少参数的自定义删除器

来自分类Dev

使用std :: function对象将自定义删除器传递给std :: unique_ptr

来自分类Dev

使用typedef为std :: unique_ptr指定自定义默认删除器

来自分类Dev

使用C ++ 11 for()循环遍历vector <unique_ptr <mytype >>

来自分类Dev

C ++:自定义删除器状态

来自分类Dev

C ++入门手册第5版:shared_ptr的删除器和unique_ptr的删除器之间的区别

来自分类Dev

C ++入门手册第5版:shared_ptr的删除器和unique_ptr的删除器之间的区别

来自分类Dev

C ++ 11中unique_ptr的向量

来自分类Dev

将具有自定义删除器的unique_ptr移到shared_ptr

来自分类Dev

具有自定义删除器的unique_ptr构造函数被删除

来自分类Dev

如何将自定义删除器与std :: unique_ptr成员一起使用?

来自分类Dev

我可以使用自定义删除器简洁地声明std :: unique_ptr吗?

来自分类Dev

C ++ unique_ptr和多态

来自分类Dev

了解采用自定义删除器的unique_ptr的构造函数

来自分类Dev

初始化传递给unique_ptr自定义删除器的函子

来自分类Dev

如何为由 unique_ptr 管理的数组编写自定义删除器?

来自分类Dev

具有自定义删除程序类的STL向量和unique_ptr

来自分类Dev

具有自定义事件访问器的C#事件(添加和删除)

Related 相关文章

  1. 1

    用unique_ptr和自定义删除器包装C代码

  2. 2

    智能指针(unique_ptr)自定义删除器错误C2027和C2338

  3. 3

    混淆使用unique_ptr和自定义删除器

  4. 4

    unique_ptr 的有状态自定义删除器

  5. 5

    unique_ptr,自定义删除器和零规则

  6. 6

    std :: unique_ptr <T []>和自定义分配器删除器

  7. 7

    std :: unique_ptr,自定义删除器和类型更改

  8. 8

    C ++ 11 unique_ptr数组和构造函数参数

  9. 9

    C ++零规则:多态删除和unique_ptr行为

  10. 10

    使用带有unique_ptr的自定义删除器

  11. 11

    使用std :: function对象将自定义删除器传递给std :: unique_ptr

  12. 12

    std :: unique_ptr使用带有很少参数的自定义删除器

  13. 13

    使用std :: function对象将自定义删除器传递给std :: unique_ptr

  14. 14

    使用typedef为std :: unique_ptr指定自定义默认删除器

  15. 15

    使用C ++ 11 for()循环遍历vector <unique_ptr <mytype >>

  16. 16

    C ++:自定义删除器状态

  17. 17

    C ++入门手册第5版:shared_ptr的删除器和unique_ptr的删除器之间的区别

  18. 18

    C ++入门手册第5版:shared_ptr的删除器和unique_ptr的删除器之间的区别

  19. 19

    C ++ 11中unique_ptr的向量

  20. 20

    将具有自定义删除器的unique_ptr移到shared_ptr

  21. 21

    具有自定义删除器的unique_ptr构造函数被删除

  22. 22

    如何将自定义删除器与std :: unique_ptr成员一起使用?

  23. 23

    我可以使用自定义删除器简洁地声明std :: unique_ptr吗?

  24. 24

    C ++ unique_ptr和多态

  25. 25

    了解采用自定义删除器的unique_ptr的构造函数

  26. 26

    初始化传递给unique_ptr自定义删除器的函子

  27. 27

    如何为由 unique_ptr 管理的数组编写自定义删除器?

  28. 28

    具有自定义删除程序类的STL向量和unique_ptr

  29. 29

    具有自定义事件访问器的C#事件(添加和删除)

热门标签

归档