VC6中stl的list.sort问题
#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
#include <list>
using std::list;
#include <string>
using std::string;
#include <ctime>
class Person
{
public:
string name;
int age;
Person( string name, int age )
{
this->name = name;
this->age = age;
}
void PrintInfo()
{
cout << " " << name << "\t" << age << endl;
}
};
bool CompareAge( Person &p1, Person &p2 )
{
return p1.age < p2.age;
}
bool CompareName( Person &p1, Person &p2 )
{
return p1.name < p2.name;
}
void PrintPersonsInfo( list<Person> & listOut )
{
std::list<Person>::iterator li;
for ( li = listOut.begin(); li != listOut.end(); li++ )
{
((Person)*li).PrintInfo();
}
cout << endl;
}
int main(int argc, char *argv[])
{
list<Person> students;
students.push_back( Person("zhao", rand() % 20 + 5) );
students.push_back( Person("qian", rand() % 20 + 5) );
students.push_back( Person("sun", rand() % 20 + 5) );
students.push_back( Person("li", rand() % 20 + 5) );
students.push_back( Person("zhou", rand() % 20 + 5) );
students.push_back( Person("wu", rand() % 20 + 5) );
students.push_back( Person("zhen", rand() % 20 + 5) );
students.push_back( Person("wang", rand() % 20 + 5) );
cout << "the students: " << endl;
PrintPersonsInfo( students );
//for_each( students.begin(), students.end(), PrintStudentInfo );
cout << "sort of age: " << endl;
students.sort( CompareAge );
PrintPersonsInfo( students );
cout << "sort of name: " << endl;
students.sort( CompareName );
PrintPersonsInfo( students );
return 0;
}
上面这段代码,用MinGW编译通过,执行正常,但将代码拷到VC6中却不能编译,提示如下:
Compiling...
ListSort.cpp
d:\golden\src\listsort\listsort.cpp(70) : error C2664: 'void __thiscall std::list<class Person,class std::allocator<class Person> >::sort(struct std::greater<class Person>)' : cannot convert parameter 1 from 'bool (class Person &,class Person &)' to
'struct std::greater<class Person>'
No constructor could take the source type, or constructor overload resolution was ambiguous
d:\golden\src\listsort\listsort.cpp(74) : error C2664: 'void __thiscall std::list<class Person,class std::allocator<class Person> >::sort(struct std::greater<class Person>)' : cannot convert parameter 1 from 'bool (class Person &,class Person &)' to
'struct std::greater<class Person>'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing xicl6.exe.
ListSort.exe - 2 error(s), 0 warning(s)
望高手指点。