【发布时间】:2016-09-26 13:16:40
【问题描述】:
我收到一个巨大的编译错误消息
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)); }
当我将自定义比较器传递给 STL set_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
【问题讨论】:
-
你可能会想要
auto rPtr1 = ded1.get(),但这可能是无关的。 -
@MSalters 他们可能根本不需要将
ded1转换为原始指针。只需像普通指针一样使用unique_ptr:ded1->p_number。 -
阅读文档。问题很明显。
-
@BaummitAugen:还在等你的回答,如果它这么明显的话。
-
有什么理由不使用
std::vector<Deduction>BTW?这可能会容易得多。
标签: c++