【问题标题】:make_unique giving error 2248make_unique 给出错误 2248
【发布时间】:2014-10-26 23:28:46
【问题描述】:

我遇到了 make_unique 的问题,让我不知所措。

_replace_find = unique_ptr<Fl_Input>(new Fl_Input{ 80, 10, 210, 25, "Find:" });
_replace_find = make_unique<Fl_Input>(Fl_Input{ 80, 10, 210, 25, "Find:" });

当我使用 make_unique 行时,它给了我这个错误,但是当我使用另一个时,它编译得很好。据我了解,make_unique 几乎做同样的事情,但异常安全。

Error   1   error C2248: 'Fl_Widget::Fl_Widget' : cannot access private member declared in class 'Fl_Widget'    c:\program files (x86)\microsoft visual studio 12.0\vc\include\fl\fl_input_.h   488 1   hayley

我找不到任何处理 SO 上的 make_unique 或 unique_ptr 的错误。否则我不会问这个。

一如既往地感谢您的时间和建议。

【问题讨论】:

  • 我猜问题是你的新代码正在调用复制构造函数。
  • @Brian 或移动构造函数。

标签: c++ visual-c++ unique-ptr c++14


【解决方案1】:

你可能想写

std::make_unique<FlInput>(80, 10, 210, 25, "Find:")

而不是

std::make_unique<FlInput>(FlInput{80, 10, 210, 25, "Find:"})

似乎FlInput 类有一个私有副本和/或移动构造函数,使得第二种形式非法。

【讨论】:

  • 谢谢!这就是问题所在。
【解决方案2】:
_replace_find = unique_ptr<Fl_Input>(new Fl_Input{ 80, 10, 210, 25, "Find:" });
_replace_find = make_unique<Fl_Input>(Fl_Input{ 80, 10, 210, 25, "Find:" });

这些行不等价。

第一行在免费存储上创建一个Fl_input,然后用它初始化一个unique_ptr
第二个创建一个临时的Fl_input 并用它调用make_unique&lt;Fl_input&gt;,它通过调用复制/移动ctor(显然无法访问,因此出现错误)在空闲存储上创建一个新实例。

您想要的是将所有 ctor 参数提供给 make_unique&lt;Fl_input&gt;

_replace_find = make_unique<Fl_Input>(80, 10, 210, 25, "Find:");

【讨论】:

  • 谢谢!这就是问题所在。由于Dietmar 在您之前回答了我,因此我将他标记为答案。我也对你的答案投了赞成票,因为它得到了很好的解释。
猜你喜欢
  • 2016-01-18
  • 2021-11-07
  • 2020-08-18
  • 2014-08-27
  • 1970-01-01
  • 2017-12-20
  • 2012-09-25
  • 2013-03-21
  • 2016-12-02
相关资源
最近更新 更多