比较函数在一个类中的std :: sort不能编译 [英] Compare function in a class for std::sort can't compile
问题描述
我在写一个程序来获得凸包。我需要通过极角对点进行排序,我之前选择了一个 base
点,所以我写了一个成员比较函数(注意,对于每个对象 base
point不同)。但是当我将它应用到 std :: sort
时,程序无法编译。
I'm writing a program to get a convex hull. I need to sort the points by polar angle, and I chose a base
point before, so I write a member compare function ( notice that, for each object the base
point is different ). But when I'm applying it to the std::sort
, the program can't compile.
是我的程序:
class ConvexHull
{
Point p[MAXN], base;
public:
int size;
void Create(Point (&)[MAXN], const int);
bool cmp(const Point& a, const Point& b) const
{
static int tmp;
return (tmp = CrossProduct(base, a, base, b)) < 0 || (tmp == 0 && Dist(base, a) < Dist(base, b));
}
};
void ConvexHull::Create(Point (&a)[MAXN], const int n)
{
base = a[0];
for (int i = 1; i < n; ++i)
if (a[i].x < base.x || (a[i].x == base.x && a[i].y < base.y))
base = a[i];
std::sort(a, a+n, cmp);
p[0] = a[0], p[1] = a[1];
size = 2;
for (int i = 2; i < n; ++i)
{
while (size >= 2 && CrossProduct(a[i], p[size-1], a[i], p[size-2]) <= 0) --size;
p[size++] = a[i];
}
p[size++] = p[0];
}
这里是错误:
poj1113.cc: In member function 'void ConvexHull::Create(Point (&)[1000], int)':
poj1113.cc:41:24: error: no matching function for call to 'sort(Point [1000], Point*, <unresolved overloaded function type>)'
poj1113.cc:41:24: note: candidates are:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from poj1113.cc:3:
/usr/include/c++/4.7/bits/stl_algo.h:5463:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
/usr/include/c++/4.7/bits/stl_algo.h:5463:5: note: template argument deduction/substitution failed:
poj1113.cc:41:24: note: candidate expects 2 arguments, 3 provided
In file included from /usr/include/c++/4.7/algorithm:63:0,
from poj1113.cc:3:
/usr/include/c++/4.7/bits/stl_algo.h:5499:5: note: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = Point*; _Compare = bool (ConvexHull::*)(const Point&, const Point&)const]
/usr/include/c++/4.7/bits/stl_algo.h:5499:5: note: no known conversion for argument 3 from '<unresolved overloaded function type>' to 'bool (ConvexHull::*)(const Point&, const Point&)const'
如何解决?这是(我的意思是使基础
成员)一个坏deisgn?
How to fix it? And is this ( I mean make the base
member ) a bad deisgn?
推荐答案
问题是您的 cmp
方法需要 static
。原因是非静态方法需要一个不可见的第一个参数, this
指针。 std :: sort
函数不会传递此额外的参数。
The problem is that your cmp
method needs to be static
. The reason is that non-static methods expect an invisible first argument, the this
pointer. The std::sort
function do not pass this extra argument.
引用成员变量,你不能使函数 static
,但还有其他方法来解决这个问题。我建议使用新的C ++ 11标准功能, std :: bind
:
Since you reference member variables you can't make the function static
, but there are other ways to solve this. I recommend using a new C++11 standard functionality, std::bind
:
std::sort(a, a+n, std::bind(&ConvexHull::cmp, this));
std :: bind
可调用对象,将第一个参数设置为 this
,以便在调用时正确。
The std::bind
call creates a callable object, setting the first parameter to this
so it will be correct when being called.
这篇关于比较函数在一个类中的std :: sort不能编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!