make_unique给出错误2248

时间:2014-10-26 23:28:47

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

我遇到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相关的错误。我不会这样问。

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

2 个答案:

答案 0 :(得分:6)

你可能想写

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

而不是

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

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

答案 1 :(得分:5)

_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<Fl_input>,它通过调用copy / move ctor(这显然是不可访问的,因此错误)在free store上创建一个新实例。

你想要的是给make_unique<Fl_input>的所有ctor参数:

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