accumulate()与运算符重载

时间:2018-02-06 16:52:28

标签: c++ operator-overloading accumulate

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

我有一个包含

的类订单
private:
Customer cust;
std::vector<Purchase> vP;

以及使用

购买的课程
private:
    string name;
    double unit_price;
    int count;

我想使用accumulate()将Orders中所有购买的价格(这意味着unit_price * count)加在一起。我重载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;
} 

和累积我打电话

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

1 个答案:

答案 0 :(得分:1)

累加器谓词应该有签名favorite

简单的积累方法是:

# PostgreSQL
CREATE TABLE categories_services (
  id SERIAL PRIMARY KEY,
  category_id integer REFERENCES categories NOT NULL,
  service_id integer REFERENCES services NOT NULL,
  created_at timestamp NOT NULL DEFAULT now(),
  updated_at timestamp NOT NULL DEFAULT now()
);

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

double(double, const Order&)