Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在共享\u ptr中使用自定义删除器_C++_Templates_Shared Ptr - Fatal编程技术网

C++ 在共享\u ptr中使用自定义删除器

C++ 在共享\u ptr中使用自定义删除器,c++,templates,shared-ptr,C++,Templates,Shared Ptr,我定义了一个类,它有一个成员模板,代替std::shared_ptr的默认deleter: class DebugDelete { public: DebugDelete(std::ostream &s = std::cerr): os(s) { } // as with any function template, the type of T is deduced by the compiler template <type

我定义了一个类,它有一个成员模板,代替std::shared_ptr的默认deleter:

class DebugDelete {
    public:
        DebugDelete(std::ostream &s = std::cerr): os(s) { }
        // as with any function template, the type of T is deduced by the compiler
        template <typename T> void operator()(T *p) const
        {
           os << "deleting unique_ptr" << std::endl;
           delete p;
        }
    private:
        std::ostream &os;
};
当我将其应用于以下代码时,报告了一些错误:

class A {
    public:
        // [Error] class 'A' does not have any field named 'r'
        A(std::shared_ptr<std::set<int>> p): r(p) { } // 1: How can I use self-defined deleter to initialize r in constructor
        A(int i): s(new std::set<int>, DebugDelete()) { } // 2: OK, what is the difference between this constructor and 3
    private:
        // [Error] expected identifier before 'new'
        // [Error] expected ',' or '...' before 'new'
        std::shared_ptr<std::set<int>> r(new std::set<int>, DebugDelete()); // 3: error
        std::shared_ptr<std::set<int>> s;
};

在初始化列表中,您可以使用自定义的删除器,如

shared_ptr<set<int>> r = shared_ptr<set<int>>(new set<int>, DebugDelete());

您不应该使用一个共享ptr来初始化另一个。

我怀疑您收到的许多错误与自定义删除无关。当然,在您的问题中包含实际的错误消息(应该总是这样做)将证实这一点。@WhozCraig我已经在代码中添加了错误消息。您不能用这种语法初始化类中的成员。试试大括号或相等的初始值设定项。@n.m.但在成员函数中,我标记为3的行是有效的,就像我注释的Blue moe的答案一样。在成员函数中是一回事。不在成员函数中是另一回事。无论如何,只要始终使用大括号:std::shared_ptr{new std::set,DebugDelete};我按照你说的修改了代码,并编译了它。但是我还定义了类a的成员函数-void B{shared_ptr ptrnew set,DebugDelete;}-公共编译和私有编译。这是怎么发生的?你的函数void B是一个名为ptr的共享ptr构造函数。我的答案是分配一个成员变量。在C++11标准中,成员变量可以在类定义中初始化。示例:n.m.的答案是初始化成员变量的另一种方法。初始化器列出,请参见:您的映射m可以像shared_ptr ptr=shared_ptrnew set、DebugDelete一样使用;m[onestring]=ptr,或typedef映射::value_type S_SP;m={S_SPstring,ptr,另一个S_SP…};map的元素是键、值对,您只需传递一对,它就会编译。我已经将我的代码修复为您的方法。