晚上好,专家
我正在以通用的方式进行编程练习。我理解如何使用不同的数据类型对不同的对象进行排序,但是我正在努力如何对类进行排序
这是我的代码:
#include <iostream>
#include<string>
using namespace std;
class Employee
{
private:
int idNum;
string lastName;
int hireDate;
int verifyDate(int);
public:
Employee();
Employee(int,string,int);
void displayData();
void setLastName(string);
void setIdNum(int);
void setHireDate(int);
void setGradePointAverage(double);
};
Employee::Employee()
{
idNum=0;
lastName="";
hireDate=0;
}
Employee::Employee(int idNum, string lastName, int hireDate)
{
this->idNum=idNum;
this->lastName=lastName;
this->hireDate=hireDate;
}
void Employee::displayData()
{
cout<<"\nEmployee Data:";
cout<<"\nID: "<<idNum;
cout<<"\nLastName: "<<lastName;
cout<<"\nHireDate: "<<hireDate;
}
void Employee::setLastName(string lastName)
{
this->lastName=lastName;
}
void Employee::setIdNum(int idNum)
{
this->idNum=idNum;
}
void Employee::setHireDate(int hireDate)
{
this->hireDate=hireDate;
}
void Employee::setGradePointAverage(double grade)
{
double new_grade;
new_grade=grade;
}
template <class T>
void selectionSort (T data[], int n){
T temp;
for(int i=0, j, least; i < n-1; i++){
for( j = i+1, least = i; j < n; j++ ){
if ( data[j] < data[least]){
least = j;
}
}
temp = data[least];
data[least] = data[i];
data[i] = temp;
}
}
int main()
{
Employee e[3];
e[0].setIdNum(337322);
e[1].setIdNum(3539854);
e[2].setIdNum(1224567);
e[0].setLastName("Marlen");
e[1].setLastName("Oleg");
e[2].setLastName("Test");
e[0].setHireDate(25061989);
e[1].setHireDate(30001990);
e[2].setHireDate(12122012);
for (int i=0; i<3;i++)
{
e[i].displayData();
cout<<"\n";
}
selectionSort(e,3);
return 0;
}
当我调用我的selectionSort函数时,编译器会给我以下错误:
Error 1 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'Employee' c:\users\gulmira\documents\visual studio 2010\projects\csci203_lab1\csci203_lab1\task6.cpp 74
Error 2 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'Employee' c:\users\gulmira\documents\visual studio 2010\projects\csci203_lab1\csci203_lab1\task6.cpp 74
Error 3 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'Employee' c:\users\gulmira\documents\visual studio 2010\projects\csci203_lab1\csci203_lab1\task6.cpp 74
请提出如何解决此问题的建议,在此先感谢
答案 0 :(得分:2)
错误指出,operator<
没有Employee
,但您正在尝试在排序功能中使用它。
您需要选择一个排序标准(例如按员工ID数字)并相应地实施operator<
。
E.g:
bool Employee::operator<(const Employee& rhs) const {
return idNum < rhs.idNum;
}
如果您想给调用者选择排序条件,可以像std::sort()
那样通过允许传递一个将用于比较的函子或函数指针来实现:
template <class T, class Comp>
void selectionSort (T data[], int n, Comp comp){
// ...
if (comp(data[j], data[least])) {
// ...
...然后你可以...像这样使用:
bool orderEmployeesByLastName(const Employee& a, const Employee& b) {
return std::lexicographical_compare(
a.getLastName().begin(), a.getLastName.end(),
b.getLastName().begin(), b.getLastName.end());
}
// ...
selectionSort(e, 3, &orderEmployeesByLastName);
答案 1 :(得分:1)
你需要实现&lt;员工类的操作员。不要指望编译器知道如何为您执行此操作。您需要告诉它您希望该操作员为该类做什么。