make_unique给出错误2248

Jas*_*all 1 c++ unique-ptr visual-c++ 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:" });
Run Code Online (Sandbox Code Playgroud)

当我使用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
Run Code Online (Sandbox Code Playgroud)

在SO上,我找不到任何与make_unique或unique_ptr相关的错误.我不会这样问.

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

Die*_*ühl 6

你可能想写

std::make_unique<FlInput>(80, 10, 210, 25, "Find:")
Run Code Online (Sandbox Code Playgroud)

代替

std::make_unique<FlInput>(FlInput{80, 10, 210, 25, "Find:"})
Run Code Online (Sandbox Code Playgroud)

似乎该类FlInput具有私有副本和/或移动构造函数,使第二种形式非法.


Ded*_*tor 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:" });
Run Code Online (Sandbox Code Playgroud)

那些线不等同.

第一行在Fl_input免费商店中创建一个,然后unique_ptr用它初始化a .
第二个创建一个临时的Fl_inputmake_unique<Fl_input>使用它来调用,它通过调用copy/move ctor(这显然是不可访问的,因此错误)在free store上创建一个新实例.

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

_replace_find = make_unique<Fl_Input>(80, 10, 210, 25, "Find:");
Run Code Online (Sandbox Code Playgroud)