[英]why can't i pass a reference to my unique_ptr?
I have a vector of std::unique_ptr<k_ctrl_t>
and I want to pass a reference of the unique_ptr
within this vector to another function:我有一个
std::unique_ptr<k_ctrl_t>
的向量,我想将此向量中的unique_ptr
的引用传递给另一个 function:
std::vector<std::unique_ptr<k_ctrl_t>> _kitchens;
for (int i = 0; i < (int)_kitchens.size(); i++)
{
if (FD_ISSET(_kitchens[i]->socket, &this->_readfds))
{
handleKitchenRet(std::cref<std::unique_ptr<k_ctrl_t>>(_kitchens[i]));
}
}
void Reception::handleKitchenRet(std::unique_ptr<k_ctrl_t> kitch)
{
...
}
But this doesn't work.但这不起作用。 How could I do that?
我怎么能那样做?
The error message:错误信息:
error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = kitchen_control_s; _Dp = std::default_delete<kitchen_control_s>]' 169 | handleKitchenRet(std::ref<uKCtrl>(_kitchens[i]));
void Reception::handleKitchenRet(std::unique_ptr<k_ctrl_t> kitch)
is taking a unique_ptr
by value .正在按value获取
unique_ptr
。
To take it by reference , change the signature to要作为参考,请将签名更改为
void Reception::handleKitchenRet(std::unique_ptr<k_ctrl_t>& kitch)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.