c++ - 如何重置 shared_ptr?

标签 c++ stl c++11 shared-ptr

我正在尝试这样做(使用自定义类和来自 #include <memory> 的 STL shared_ptr):

shared_ptr<Label> BufLbl;

BufLbl = allocate_shared<Label>(Label());
BufLbl->YCoord = 3;
BufLbl->XCoord = 2; 
BufLbl->Width = 5;
BufLbl->Data = "Day 1";
Screen.Controls.push_back(BufLbl);

BufLbl = allocate_shared<Label>(Label());
BufLbl->YCoord = 4;
BufLbl->XCoord = 6; 
BufLbl->Width = 1;
BufLbl->Data = "2";
Screen.Controls.push_back(BufLbl);

<repeat>

我收到这个错误:

error C2903: 'rebind' : symbol is neither a class template nor a function template

我做错了什么?

最佳答案

你在滥用 allocate_shared,这不是你想的那样。

你需要的是make_shared,像这样:

BufLbl = std::make_shared<Label>();

关于c++ - 如何重置 shared_ptr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13355422/

相关文章:

c++ - 使用分配器 C++ 创建元组

c++ - 数组参数的完整性检查(strlen 等)

c++ - 如何在双端队列中使用用户定义的类?

c++ - 有没有办法为 std::map 中小于给定键的第一个元素找到反向迭代器?

c++ - C++11标准中这条语句的含义是什么?

c++ - Objective-C 集成 C 库并传递字符串值 - ARC 错误

c++ - c++数据结构,可根据成员的值自动对对象进行排序

c++ - 在 C++11 中同步整个类

c++11 - 来自 ubuntu 16.04 repo 的 c++ 库 - 编译器选项

c++ - 移动字符数组的值以注册 x86 内联汇编