c++ - 具有运算符重载的 accumulate()

标签 c++ operator-overloading accumulate

我有一个关于 accumulate() 和运算符重载的问题。

我有一个类 Order 包含

private:
Customer cust;
std::vector<Purchase> vP;

和一个类 Purchase with

private:
    string name;
    double unit_price;
    int count;

我想使用 accumulate() 将所有购买的价格(这意味着 unit_price*count)添加到一个带有订单的 vector 中。我重载 Order 的 + 运算符的解决方案不起作用。这是它的样子:

double Order::operator+(Order& p) {

    double gesamt{};
    gesamt=(gesamtpreisEinerPerson()+p.gesamtpreisEinerPerson());

    return gesamt;
}

double Order::gesamtpreisEinerPerson(){
    double kosten=0.0;

    for (int i=0; i<vP.size(); ++i){
    kosten+=(vP.at(i).getPrice()*vP.at(i).getCount());        
    }   
    return kosten;
} 

我调用accumulate

double totalPrice(vector<Order> vectOrd) {

    double d = accumulate(vectOrd.begin(), vectOrd.end(), 0.0);

    return d;
    }

所以我的问题是:为什么我不能用运算符重载来解决累加问题?它一般不起作用还是我的代码有问题?非常感谢!

错误信息是

In file included from main.cpp:5:
In file included from ./global.hpp:16:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/numeric:81:25: error: invalid operands to binary expression ('double' and 'Order')
        __init = __init + *__first;
                 ~~~~~~ ^ ~~~~~~~~
./global.hpp:272:16: note: in instantiation of function template specialization 'std::__1::accumulate<std::__1::__wrap_iter<Order *>, double>' requested here
    double d = accumulate(vectOrd.begin(), vectOrd.end(), 0.0);
               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:743:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'Order'
operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:1163:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against 'Order'
operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:1576:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against 'Order'
operator+(typename __wrap_iter<_Iter>::difference_type __n,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3698:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3711:1: note: candidate template ignored: could not match 'const _CharT *' against 'double'
operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3723:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'Order'
operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3734:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3746:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3760:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3768:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3776:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3784:1: note: candidate template ignored: could not match 'const _CharT *' against 'double'
operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3792:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'Order'
operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3801:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3809:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
^
1 error generated.
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

最佳答案

累加器谓词应具有签名 double(double, const Order&):

进行积累的简单方法是:

double d = accumulate(vectOrd.begin(),
                      vectOrd.end(),
                      0.0,
                      [](double acc, const Order& order) {
                          return acc + order.gesamtpreisEinerPerson();
                      });

如果你真的想使用运算符重载,你必须实现(自由函数):

double operator+ (double d, const Order& order)
{
    return d + order.gesamtpreisEinerPerson();
}

关于c++ - 具有运算符重载的 accumulate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648086/

相关文章:

C++ 如何加入多播 mpeg 流?

c++ - 常量和非常量运算符重载

Mysql - 逐行累计统计总数

android - ndk 中的 LocalBroadcastManager

C++复制构造函数和相等运算符

c++ - 分配顺序与初始化顺序

c++ - #在c++中定义一个特殊的运算符

C++ 标准没有说明枚举类的 operator!=() 和 operator==()

C++:我的累加器有问题

python - XPath : is it possible? (Python) 中的累加器