0

Good evening, experts

I am working on the exercise in programming in generic way. I understood how to to sort the different objects with different data types, however I am struggling how to sort classes

here is my code:

#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;
}

when i call my selectionSort function the compiler gives me the following errors:

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

Please suggest an idea for how to solve this, Thanks in Advance

2 Answers 2

2

As the error points out, there is no operator< for Employee, but you are trying to use it in your sorting function.
You need to choose an ordering criteria (e.g. numerically by employee id) and implement operator< accordingly.

E.g.:

bool Employee::operator<(const Employee& rhs) const { 
    return idNum < rhs.idNum; 
}

If you want to give the caller the choice of the ordering criteria, you can do it like std::sort() by allowing a functor or function pointer to be passed that will be used for the comparisons:

template <class T, class Comp>
void selectionSort (T data[], int n, Comp comp){
    // ...
    if (comp(data[j], data[least])) {
    // ...

... which you could then e.g. use like this:

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);
Sign up to request clarification or add additional context in comments.

2 Comments

so as i understood do i need to overload operators? and how can i access i sort numerically and access it?
so how I can sort by names if already defined operator > to sort by numbers?
1

You need to implement the < operator for your employee class. Don't expect the compiler to know how to do this for you. You need to tell it what you want that operator to do for the class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.