怎么让下面这段程序编译通过啊???
#include <iostream>
#include <string>
#include <set>
#include <functional>
#include <algorithm>
using namespace std;
class Employee
{
public:
Employee(size_t id = 0, const string& name = " "):_id(id), _name(name) {}
size_t getID() { return _id; }
private:
size_t _id;
string _name;
};
IDLess :public binary_function<Employee, Employee, bool>
{
public:
bool operator()(const Employee& lhs, const Employee& rhs) const
{
return lhs.getID() < rhs.getID();
}
};
int main()
{
Employee emp1(2009102344, "蔡小东"), emp2(2009103333, "陈海明"), emp3(2009103504, "李佳涛");
set<Employee, IDLess> set1;
set1.insert(emp1);
set1.insert(emp2);
set1.insert(emp3);
for_each(set1.begin(), set1.end(), mem_fun(&Employee::print));
}
[解决办法]
#include <iostream>#include <string>#include <set>#include <functional>#include <algorithm>using namespace std;class Employee{public: Employee(size_t id = 0, const string& name = " "):_id(id), _name(name) {} size_t getID() const { return _id; } void print() const { cout << _id << " " << _name << endl; } bool operator < (const Employee &obj) const {return _id < obj._id;}private: size_t _id; string _name;};class IDLess :public binary_function<Employee, Employee, bool>{public: bool operator()(const Employee& lhs, const Employee& rhs) const { return lhs.getID() < rhs.getID(); }};int main(){ Employee emp1(2009102344, ", emp2(2009103333, ", emp3(2009103504, "; set<Employee, IDLess> set1; set1.insert(emp1); set1.insert(emp2); set1.insert(emp3); for_each(set1.begin(), set1.end(), mem_fun_ref(&Employee::print));}