Click to See Complete Forum and Search --> : Compare


ami
01-14-2007, 04:02 PM
Hello,

I have a struct and a function compare :-

struct Student_info
{
std::string name;
};

bool compare(const Student_info& x, const Student_info& y)
{
return x.name < y.name;
}

When I call the function with a vector all works ok :-

vector<Student_info> students;
Student_info record;
sort(students.begin(), students.end(), compare);

My book I should call compare like this for lists :-

list<Student_info> students;
Student_info record;
students.sort(compare);

However, I'm getting compilation error :-
error C2664: 'void __thiscall std::list<struct Student_info,class std::allocator<struct Student_info> >::sort(struct std::greater<struct Student_info>)' : cannot convert parameter 1 from 'bool (const struct Studen
t_info &,const struct Student_info &)' to 'struct std::greater<struct Student_info>'
No constructor could take the source type, or constructor overload resolution was ambiguous

Can someone please help ?

Many thanks.

dcwexter
01-15-2007, 05:15 AM
What version of VS do you use?

ami
01-15-2007, 05:43 AM
I'm using VC++ version 6.0

Any ideas ?

Danny
01-15-2007, 06:20 AM
VC++ is notoriously buggy with respect to namespaces. Did you downloadthe latest SPs?

ami
01-15-2007, 06:32 AM
Can you please tell me where I can download from and what to look for ?

Viorel
01-15-2007, 07:29 AM
Your sample works in newer Visual Studio 2005, but older STL library, supplied with VC++ 6, seems to require another kind of parameter.

Try the following declaration of compare predicate:
template< >
struct std::greater< Student_info >
{
bool operator () ( const Student_info & x, const Student_info & y) const
{
return x.name > y.name;
}
};
then use it in this manner:
std::list< Student_info > students;
. . .
students.sort(std::greater< Student_info >());
I hope it works.

Danny
01-15-2007, 08:02 AM
Can you please tell me where I can download from and what to look for ?
MSDN or Microsoft->Developers sites should have it. My only concern is that since VC++ 6.0 is no longer supported, thye might have removed the SPs from their site. Is there a particular reason for using such an outdated compiler? You can download Visual Studio Express edition for free. It's lightyears ahead of VC++ 6.0 and if you're a C++ newcomer, it's always a good idea to start with a standard compliant compiler.