简体   繁体   English

为什么我不能传递对我的 unique_ptr 的引用?

[英]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.

相关问题 我可以将unique_ptr的引用传递给函数吗? - Can I pass a unique_ptr's reference to a function? 为什么我不能返回对使用模板创建的派生 class 的 unique_ptr 的引用? - Why can't I return a reference to a unique_ptr of a derived class created with a template? 为什么我不能从一对中返回unique_ptr? - Why can't I return a unique_ptr from a pair? 为什么不能将funique_ptr与fread一起使用? - Why can't I use unique_ptr with fread? 如何以多态方式通过引用传递unique_ptr? - How to pass unique_ptr by reference polymorphically? 我应该通过 unique_ptr<t> 在这里参考?</t> - Should I be passing unique_ptr<T> by reference here? unique_ptr:所以我不能再引用已删除的函数 - unique_ptr: So I can't reference to a deleted function anymore 为什么 T* 可以在寄存器中传递,但 unique_ptr<t> 不能?</t> - Why can a T* be passed in register, but a unique_ptr<T> cannot? 如何将 std::unique_ptr 传递给 function - How can I pass std::unique_ptr into a function 我可以传递一个 std::vector 吗<std::unique_ptr<t> > 作为一个没有额外分配的原始指针向量? </std::unique_ptr<t> - Can I pass a std::vector<std::unique_ptr<T>> as a vector of raw pointer without extra allocations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM