stl中sort()排序的问题
我用C++编了如下代码片断:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>
#include "Advertisement.h "
#include "Listing.h "
using namespace std;
Listing :: Listing(const Listing& li)
{
copy(li.objects.begin(), li.objects.end(), objects.begin());
}
// return a sorted copy of this Listing
Listing Listing :: sort(string field)
{
Listing temp = *this;
if(strcmp(field.data(), "email ") == 0)
{
std::sort((*temp.begin()), (*temp.end()), less_email);
}
return temp;
}
bool Listing :: less_email(const Advertisement* a1, const Advertisement* a2)
{
return (strcmp(a1-> getEmail().data(), a2-> getEmail().data()) == -1);
}
编译的时候一直出错:
error C2664: 'void __cdecl std::sort(class Advertisement *,class Advertisement *,bool (__thiscall *)(const class Advertisement *,const class Advertisement *)) ' :
cannot convert parameter 3 from 'bool (const class Advertisement *,const class Advertisement *) ' to 'bool (__thiscall *)(const class Advertisement *,const class Advertisement *) '
None of the functions with this name in scope match the target type
Error executing cl.exe.
麻烦各位解答一下!急!!!
[解决办法]
less_email写的不对,改成类的static成员函数。另外参数好像也有问题,改成const Advertisement & 试下。
[解决办法]
less_email是成员函数,调用不了。
可以定义为友元函数。
你也可以重载Listing的运算符 <
然后直接调用std::sort((*temp.begin()), (*temp.end()));