Sort a vector of Employee pointers

Hi,
I am trying to sort a vector of Employee pointers and should display those values based on the name in an order.Below are the classes:

//Company.h

class Company
{
private:
std::vector<Employee*> e; //Vector of Employee pointers.
};

Company.cpp

void Company::dislayEmployee() const
{
sort(e.begin(),e.end(),CompareByName); //Using CompareByName which is a user-defined function.
vector<Employee*>::const_iterator iter;
for(iter=e.begin();iter!=e.end();++iter)
{
(*iter)->display();
}
}

//Employee.h

class Employee
{

};
bool compareByName(const Employee* e1,const Employee* e2);

//Employee.cpp

bool compareByName(const Employee* e1,const Employee* e2)
{
return e1->getName() < e2->getName();
}

Following are the errors with this code:
c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3660): error C3892: '_First' : you cannot assign to a variable that is const
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3677) : see reference to function template instantiation 'void std::_Insertion_sort1<_BidIt,bool(__cdecl *)(const Employee *,const Employee *),Employee*>(_BidIt,_BidIt,_Pr,_Ty *)' being compiled
1> with
1> [
1> _BidIt=Employee *const *,
1> _Pr=bool (__cdecl *)(const Employee *,const Employee *),
1> _Ty=Employee *
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3797) : see reference to function template instantiation 'void std::_Insertion_sort<_RanIt,bool(__cdecl *)(const Employee *,const Employee *)>(_BidIt,_BidIt,_Pr)' being compiled
1> with
1> [
1> _RanIt=Employee *const *,
1> _BidIt=Employee *const *,
1> _Pr=bool (__cdecl *)(const Employee *,const Employee *)
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3806) : see reference to function template instantiation 'void std::_Sort<Employee*const *,__w64 int,bool(__cdecl *)(const Employee *,const Employee *)>(_RanIt,_RanIt,_Diff,_Pr)' being compiled
1> with
1> [
1> _RanIt=Employee *const *,
1> _Diff=__w64 int,
1> _Pr=bool (__cdecl *)(const Employee *,const Employee *)
1> ]
1>
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3667): error C3892: '_Next1' : you cannot assign to a variable that is const
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3668): error C3892: '_Next1' : you cannot assign to a variable that is const
1> Employee.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Would like to know what are the modifications to be done for the above code.
void Company::dislayEmployee() const is a const member function;
it is not allowed to modify any of the members.

std::sort() rearranges the sequence; the sequence must be a modifiable sequence.

Dropping the const qualifier from dislayEmployee() would compromise const-correctness.
Work around - make a copy and modify the copy.
1
2
3
4
5
6
7
void Company::dislayEmployee() const
{
    std::vector temp( e ) ;
    std::sort( temp.begin(), temp.end(), CompareByName ) ;
    for( std::vector<Employee*>::iterator iter=temp.begin() ; iter!=temp.end(); ++iter ) 
                       (*iter)->display() ;
}
Hi JLBorges,
Thanks for your response.It worked.
Topic archived. No new replies allowed.