Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用运算符重载累加()_C++_Operator Overloading_Accumulate - Fatal编程技术网

C++ 使用运算符重载累加()

C++ 使用运算符重载累加(),c++,operator-overloading,accumulate,C++,Operator Overloading,Accumulate,我有一个关于accumulate()和运算符重载的问题 我有一个包含 private: Customer cust; std::vector<Purchase> vP; 我想使用accumulate()将订单向量中所有采购的价格(即单价*计数)相加。我的解决方案是重载顺序的+运算符,但它不起作用。这就是它的样子: double Order::operator+(Order& p) { double gesamt{}; gesamt=(gesamtpreis

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

我有一个包含

private:
Customer cust;
std::vector<Purchase> vP;
我想使用accumulate()将订单向量中所有采购的价格(即单价*计数)相加。我的解决方案是重载顺序的+运算符,但它不起作用。这就是它的样子:

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 Order::operator+(Order&p){
双gesamt{};
gesamt=(gesamtpreisenerperson()+p.gesamtpreisenerperson());
返回gesamt;
}
双顺序::gesamtpreisenerperson(){
双kosten=0.0;

对于(int i=0;i累加器谓词应具有签名
double(double,const Order&)

进行积累的简单方法是:

double d = accumulate(vectOrd.begin(),
                      vectOrd.end(),
                      0.0,
                      [](double acc, const Order& order) {
                          return acc + order.gesamtpreisEinerPerson();
                      });
如果确实要使用运算符重载,则必须实现(free函数):


可能相关,也可能不相关,但是通过采取
顺序&
并且不使函数
常量
,你是说不允许任何人调用它,除非LHS是一个非常量对象,RHS不是临时的,也不是常量。对于一个不应该修改任何操作数的操作符来说,这些都是非常不正常的要求。这不管用吗k是什么意思?哦,是的,谢谢,我把它改成了双运算符+(常数顺序和p)。谢谢!代码没有编译,我收到以下错误消息:复制并粘贴错误消息。谢谢你的回答!对不起,我还是个编程高手。累加器谓词到底是什么?有没有办法解决运算符重载问题?累加器谓词是
std::acculate。我使用运算符+()作为成员函数,它不允许我接受3个参数。我说的是作为自由函数(非成员函数)…左操作数应该是
double
。如果使用成员函数,第一个参数是
Order
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 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();
}