注册 登录
编程论坛 VC++/MFC

使用accumulate出现error c 2064错误

鱼游海底 发布于 2015-10-29 15:32, 3085 次点击
程序代码:
main.cpp
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include "Worker.h"
using namespace std;
int main() {
    Worker w[4] = {Worker("1405121101", "zhangsan", 18, male, 8000),
    Worker("1405121102", "lisi", 19, male, 9000),
    Worker("1405121103", "wange", 20, female,10000),
    Worker("1405121104", "xiaoli", 21, female, 9999)};

    cout << "before sort:\n";
    for_each(w, w + 4, mem_fun_ref(&Worker::Print));
    sort(w, w + 4, cmpWorkerBySalary);
    cout << "after sort:\n";
    for_each(w, w + 4, mem_fun_ref(&Worker::Print));

    Worker *p = max_element(w, w + 4, cmpWorkerBySalary);
    cout << "max_salary of :" << p -> GetName() << endl;
    p = min_element(w, w + 4, cmpWorkerBySalary1);
    cout << "min_salary of :" << p -> GetName() << endl;
    cout << "average salary:" << accumulate(w, w + 4, double(0), AddSalary) / 4.0
        << endl;
    if(Man_salary(w, 4) < Woman_salary(w, 4)) cout << "average :man < woman.\n";
    if(Man_salary(w, 4) > Woman_salary(w, 4)) cout << "average :man < woman.\n";
    if(Man_salary(w, 4) == Woman_salary(w, 4)) cout << "average :man < woman.\n";
}
程序代码:
Worker.h
#ifndef WORKER_CLASS
#define WORKER_CLASS
#include <iostream>
#include <string>
using namespace std;
enum Gender {female, male};
class Worker {
private:
    string id;
    string name;
    int age;
    Gender gender;
    double salary;
public:
    Worker(const char* sid = "", const char* sn = "", int na = 0,
        Gender g = female, double sa = 0);
    Worker(const Worker& w);
    Worker& operator = (const Worker& rhs);
    double GetSalary() const {return salary;}
    Gender GetGender() const {return gender;}
    string GetName() const {return name;}

    void Print(ostream& os = cout);
};
bool cmpWorkerBySalary(const Worker& a, const Worker& b);
bool cmpWorkerBySalary1(const Worker& a, const Worker& b);
double AddSalary(double d, const Worker& s);
double Man_salary(Worker *p, int n);
double Woman_salary(Worker *p, int n);
#endif
程序代码:
Worker.cpp
#include <iostream>
#include <numeric>
#include "Worker.h"

bool cmpWorkerBySalary(const Worker& a, const Worker& b) {
    return a.GetSalary() > b.GetSalary();
}
bool cmpWorkerBySalary1(const Worker& a, const Worker& b) {
    return a.GetSalary() < b.GetSalary();
}
double AddSalary(double d, const Worker& s) {
    return d + s.GetSalary();
}
double Man_salary(Worker *p, int n) {
    int m = 0;
    for(int i = 0; i < n; ++i) {
        if((p + i) -> GetGender() == male) ++m;
    }
    return accumulate(p, p + m, double(0), AddSalary) / m;
}
double Woman_salary(Worker *p, int n) {
    int m = 0;
    for(int i = 0; i < n; ++i) {
        if((p + i) -> GetGender() == female) ++m;
    }
    return accumulate(p, p + m, double(0), AddSalary) / m;
}
Worker::Worker(const char* sid, const char* sn, int na,
    Gender g, double sa):id(sid), name(sn), age(na),
    gender(g), salary(sa) {}
Worker::Worker(const Worker& w):id(w.id), name(w.name), age(w.age),
    gender(w.gender), salary(w.salary) {}
Worker& Worker::operator = (const Worker& rhs) {
    id = rhs.id;
    name = rhs.name;
    age = rhs.age;
    gender = rhs.gender;
    salary = rhs.salary;
    return *this;
}
void Worker::Print(ostream& os) {
    os << id << "  " << name << "  " << age << "  "
        << gender << "  " << salary << endl;
}
8 回复
#2
鱼游海底2015-10-29 15:32
错误信息:  错误    1    error C2064: 项不会计算为接受 1 个参数的函数    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    22
#3
鱼游海底2015-10-29 15:33
求大神指导,急救啊!!!
#4
鱼游海底2015-10-29 16:03
望大神知道啊!!!
#5
鱼游海底2015-10-29 21:35
再来顶一楼!!!
#6
诸葛欧阳2015-10-30 19:01
#7
鱼游海底2015-11-01 17:40
不要这样!!!难道是大神们觉得这种问题不值一提???
#8
鱼游海底2015-11-02 11:06
这个程序虽然长,但是比较规范,主要解决的就是类WORKER的一些功能吧!
#9
农民工2015-11-05 08:37
你这是什么代码?怎么有for_each ?
1