简体   繁体   English

为什么我收到编译错误“使用已删除的函数'std :: unique_ptr ...”

[英]Why am I getting compile error “use of deleted function 'std::unique_ptr …”

Am getting a huge compile error with message 我的消息收到了很大的编译错误

c:\mingw\include\c++\6.1.0\bits\predefined_ops.h:123:18: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Deduction; _Dp = std::default_delete<Deduction>]'
         { return bool(_M_comp(*__it1, *__it2)); }

when I pass a custom comparator to STL set_difference function. 当我将自定义比较器传递给STL set_difference函数时。

My code: 我的代码:

struct Value{
   std::string ded_code;
   float amount;
   Value(std::string code, float amt):ded_code(code), amount(amt){}
};

struct Deduction{
  std::string p_number;
  std::vector<std::unique_ptr<Value>> values;
  Deduction(string pnum, string code, float amt):p_number(pnum){ 
    auto val = std::make_unique<Value>(code, amt);
    values.emplace_back(move(val));
  }
};

class compute{
public:
   vector<unique_ptr<Deduction>> deductions;
   void fillDeductions(){
    // fill deductions
    ...
   }

};

class CompareDiff{
public:
  bool operator()(unique_ptr<Deduction>& ded1, unique_ptr<Deductions>& ded2){
    rPtr1 = ded1.get();
    rPtr2 = ded2.get();
    return ( rPtr1->p_number < rPtr2->p_number);
 }
};

...
int main(){
  ...
  // fill two deduction vectors
  Compute compA = Compute()
  compA.fillDeductions()

  Compute compB = Compute()
  compB.fillDeductions()

  vector<unique_ptr<Deduction>> diffs

  set_difference(compA.begin(), compA.end(),
                 compB.begin(), compB.end(),
             inserter(diffs, diffs.begin()), CompareDiff());

 }

Am using gcc 6.1.0 on windows 7 machine. 我在Windows 7机器上使用gcc 6.1.0。

What am I missing? 我错过了什么?

Regards. 问候。

PG PG

The chief feature of std::unqiue_ptr is that it cannot be copied. std::unqiue_ptr的主要特点是无法复制。 That's by design, and the name tells you as much. 这是设计,名称告诉你。

However, CompareDiff tries to take its arguments by value. 但是, CompareDiff尝试按值获取其参数。 That requires a copy. 这需要一份副本。 Instead, take a std::unique_ptr<..> const& - no copy is needed. 相反,采用std::unique_ptr<..> const& - 不需要复制。

The reason why you are still getting an error: 你仍然得到一个错误的原因:

std::set_difference does copy internally: std :: set_difference在内部复制

Copies the elements from the sorted range [first1, last1) which are not found in the sorted range [first2, last2) to the range beginning at d_first. 将排序范围[first1,last1]中的元素(在排序范围[first2,last2]中找不到)复制到从d_first开始的范围。

http://en.cppreference.com/w/cpp/algorithm/set_difference http://en.cppreference.com/w/cpp/algorithm/set_difference

If you want your code to compile, use std::shared_ptr instead. 如果您希望编译代码,请改用std :: shared_ptr。

Keep in mind, that the Standard Library is optimized for Objects and not for pointers. 请记住,标准库是针对对象优化的,而不是针对指针的。 You will have to do the ownership management yourself if you use pointers. 如果使用指针,则必须自己进行所有权管理。

你不能复制构造unique_ptr因为它是一个已删除的函数 ,你可以移动唯一的指针来转移所有权,但是你需要一个函子来比较你需要通过引用传递那些unique_ptr的东西。

Use a unique_ptr<x> to express that a function assumes ownership of an x . 使用unique_ptr<x>表示函数承担x所有权。

Use a shared_ptr<x> to express that a function is part owner of an x . 使用shared_ptr<x>表示函数是x部分所有者。

If you indeed want to pass a unique_ptr and transfer ownership, you should move the smart pointer into the function argument. 如果您确实要传递unique_ptr并转移所有权,则应move智能指针move到函数参数中。

More notes on passing smart pointers , and Herb Sutter has some good thoughts in this CppCon talk . 关于传递智能指针的更多注释 ,Herb Sutter在这个CppCon讲话中有一些好的想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用已删除的函数std :: unique_ptr - use of a deleted function std::unique_ptr 错误:使用已删除的 function std::unique_ptr - Error: Use of deleted function std::unique_ptr 错误:使用已删除的 function 'std::unique_ptr&lt;_Tp, _Dp&gt;::unique_ptr - error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr c ++ 14 unique_ptr并使用删除的函数&#39;std :: unique-ptr&#39;使用unique_ptr错误 - c++14 unique_ptr and make unique_ptr error use of deleted function 'std::unique-ptr' 奇怪的错误:当没有指针真正创建时,使用已删除的函数&#39;std :: unique_ptr &lt;_Tp,_Dp&gt; :: unique_ptr - strange error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr when no pointers really created class 中的 std::unique_ptr,试图引用已删除的 function 错误 - std::unique_ptr in class, attempting to reference a deleted function error 在移动构造函数中的unique_ptr上调用std :: move时出现“错误:使用已删除的函数” - “error: use of deleted function” when calling std::move on unique_ptr in move constructor 初始化 unique_ptr 会导致“错误:使用已删除函数”,即使它是“std::move”ed - Initializing unique_ptr causes an "error: use of deleted function" even though it's "std::move"ed 删除函数unique_ptr的使用 - Use of deleted function unique_ptr 我是否需要在 std::function 上使用 unique_ptr - Do I need to use unique_ptr over std::function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM