如何重置共享_ptr

How to reset a shared_ptr?

本文关键字:ptr 共享 何重置      更新时间:2023-10-16

我正在尝试执行此操作(使用自定义类,而 #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>();