Умом понимаю о чем ошибка, но не пойму почему в Интернете 33 примера где делают как я и у них все работает
Есть такой класс. А компаратор не хочет компилироваться
Code: Select all
// Employee.h
#ifndef Employee_H
#define Employee_H
#include <string>
#include <iostream>
using namespace std;
class Employee
{
private:
int employeeId;
string firstName;
string lastName;
int age;
string department;
string position;
public:
Employee();
Employee(int id, string firstN, string lastN);
Employee(int id, string firstN, string lastN, int age);
Employee(int id, string firstN, string lastN, int age, string dept, string pos);
virtual ~Employee();
void setEmployeeId(int id);
void setFirstName(string n);
void setLastName(string n);
void setAge(int eAge);
void setDepartment(string dept);
void setPosition(string pos);
int getEmployeeId() const;
string getFirstName() const;
string getLastName() const;
int getAge() const;
string getDepartment() const;
string getPosition() const;
bool operator<(const Employee& e);
friend std::ostream &operator << (std::ostream &os, Employee const &m) {
return std::cout << m.employeeId << " " << m.firstName << " " << m.lastName << "\n";
}
};
#endif
Code: Select all
#include "Employee.h"
#include <sstream>
#include <iostream>
#include <string>
Employee::Employee()
{
employeeId = 0;
firstName = " ";
lastName = " ";
age = 0;
department = " ";
position = " ";
}
Employee::Employee(int id, string firstN, string lastN)
{
employeeId = id;
firstName = firstN;
lastName = lastN;
age = 0;
department = " ";
position = " ";
}
Employee::Employee(int id, string firstN, string lastN, int age)
{
employeeId = id;
firstName = firstN;
lastName = lastN;
age = age;
department = " ";
position = " ";
}
Employee::Employee(int id, string firstN, string lastN, int age, string dept, string pos)
{
employeeId = id;
firstName = firstN;
lastName = lastN;
age = age;
department = dept;
position = pos;
}
Employee::~Employee()
{
}
void Employee::setEmployeeId(int id)
{
employeeId = id;
}
void Employee::setFirstName(string n)
{
firstName = n;
}
void Employee::setLastName(string n)
{
lastName = n;
}
void Employee::setAge(int eAge)
{
age = eAge;
}
void Employee::setDepartment(string dept)
{
department = dept;
}
void Employee::setPosition(string pos)
{
position = pos;
}
int Employee::getEmployeeId() const
{
return employeeId;
}
string Employee::getFirstName() const
{
return firstName;
}
string Employee::getLastName() const
{
return lastName;
}
int Employee::getAge() const
{
return age;
}
string Employee::getDepartment() const
{
return department;
}
string Employee::getPosition() const
{
return position;
}
bool Employee::operator < (const Employee& e) {
return employeeId < e.employeeId;
}
Code: Select all
// project1.cpp : Defines the entry point for the console application.
//
#include "Employee.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class EmployeeComparator
{
bool operator()(const Employee& a, const Employee& b)
{
return a.getEmployeeId < b.getEmployeeId;
}
};
int main()
{
Employee employeeList[5] = {
Employee(3, "John", "Reed",25 , "accounting", "clerk"),
Employee(2, "Mary", "Jackson", 40 , "hr", "manager"),
Employee(5, "Tom", "Bregg ", 30 , "it", "programmer"),
Employee(1, "Dan", "Root", 45 , "it", "manager"),
Employee(4, "Sally", "Robesrtson", 33 , "hr", "recruiter")
};
std::sort(std::begin(employeeList), std::end(employeeList), EmployeeComparator());
cout << "Sorted Array " << endl;
for (size_t i = 0; i != 5; ++i)
cout << employeeList[i] << " ";
}
1>project1.cpp
1>d:\code\cs302\project1\project1\project1.cpp(14): error C3867: 'Employee::getEmployeeId': non-standard syntax; use '&' to create a pointer to member
1>d:\code\cs302\project1\project1\project1.cpp(14): error C2296: '<': illegal, left operand has type 'int (__thiscall Employee::* )(void) const'
1>d:\code\cs302\project1\project1\project1.cpp(14): error C2297: '<': illegal, right operand has type 'int (__thiscall Employee::* )(void) const'
1>Done building project "project1.vcxproj" -- FAILED.