为什么我会得到编译错误“使用删除的功能'std :: unique_ptr ...”

盖特

消息出现巨大的编译错误

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)); }

当我将自定义比较器传递给STLset_difference函数时。

我的代码:

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());

 }

我在Windows 7计算机上使用gcc 6.1.0。

我想念什么?

问候。

PG

特雷维尔

您仍然收到错误的原因:

std :: set_difference确实在内部复制

从排序范围[first1,last1)中找不到的元素复制到排序范围[first2,last2)中,到d_first开始的范围。

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

如果要编译代码,请改用std :: shared_ptr。

请记住,标准库针对对象而非指针进行了优化。如果使用指针,则必须自己进行所有权管理。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么std :: unique_ptr :: reset()总是noexcept?

std :: move()之后,unique_ptr会发生什么?

类型不完整的std :: unique_ptr无法编译

使用地图内的std :: unique_ptr作为键

为什么std :: shared_ptr <T> = std :: unique_ptr <T []>可以编译,而std :: shared_ptr <T []> = std :: unique_ptr <T []>不可以编译?

带有std :: unique_ptr的clang错误

使用带有分配器的std :: unique_ptr

std :: unique_ptr尝试引用已删除的函数

是否可以保证std :: unique_ptr删除顺序?

我应该使用QScopedPointer还是std :: unique_ptr?

如何在std :: copy中使用unique_ptr?

与std :: shared_ptr和std :: unique_ptr相比,std :: optional`有什么优势?

使用什么std :: optional或std :: unique_ptr

为什么std :: unique_ptr重设与赋值不同?

使用std :: unique_ptr <T>&代替std :: unique_ptr <T>有什么优势吗?

如何使用std:unique_ptr擦除包含结构的向量?

我想访问std :: unique_ptr中的特定元素

使用std :: unique_ptr创建对象数组

一些std :: unique_ptr使用和“陷阱”

错误C2248:'std :: unique_ptr <_Ty> :: unique_ptr':无法访问在类'std :: unique_ptr <_Ty>'中声明的私有成员

std :: vector <std :: unique_ptr <int>>不编译

使用std :: move(nullptr)的unique_ptr的operator =的C ++错误

使用std :: unique_ptr时出现内存泄漏

使用 std::unique_ptr 使用 CRTP 进行转换

在 QVector<std::unique_ptr<Type>> 上使用 std::find

在 Qt 上使用 std::unique_ptr

使用 std::unique_ptr<T>::unique_ptr` 在 Visual Studio 2013 中构建错误

使用 std::move 分配 unique_ptr 不起作用

为什么我需要移动 `std::unique_ptr`