PDA

View Full Version : [Qt4] QSORT



jane
24th May 2006, 11:44
Hi, I've got a problem using qSort, when I reimplement LessThan :

In my Vertical class:


QList<Plane *> Vertical::planes; //static list of Plane objects

bool lessThan( const Plane *plane1, const Plane *plane2)
{
Plane *me = Vertical::getPlanes().at(0);
return(plane1->close(me->getPosition()) < plane2->close(me->getPosition()));
}


void Vertical::sortL(const QString &str)
{
QList <Plane *> list;
qCopy(planes.begin()+1, planes.end(), list.begin());
qSort(list.begin(), list.end(),lessThan);
.......
}

QList<Plane *> Vertical::getPlanes() {
return planes;
}


and in the Plane class :

double Plane::close(const QPoint &p) const
{
return (sqrt((double)((p.x()-position.x())^2 + (p.y()-position.y())^2)));
}


And the compiler says that qsort does not look as a function that takes 2 arguments, and gives me some lines number erros in qalgorithms.h
Any ideas ???

lauranger
24th May 2006, 13:29
qStableSort, may be ?

jane
24th May 2006, 13:41
no, I ve got the same problem, but thanks

jacek
24th May 2006, 15:13
And the compiler says that qsort does not look as a function that takes 2 arguments, and gives me some lines number erros in qalgorithms.h
Could you post the exact error message?

jane
24th May 2006, 15:30
Ok no problem, but as I m french, it s not in english, here it is :

c:\Qt\src\corelib\tools\qalgorithms.h(341): error C2064: le terme ne correspond pas à une fonction qui prend les arguments 2

which means the term does not match with a function which takes the arguments 2
the line in qalgorithms.h :
if (!lessThan(*hi, *lo)) {

jane
24th May 2006, 15:40
Ok,
problem solved :
I have no idea why but I tried turning the lessThan function static and now it works ???!!!! which I don t really understands but it do compile, so ....

gfunk
25th May 2006, 00:38
qSort cannot take a member function pointer as an argument, which is a very different type than regular function pointers. Your function is really defined as:
bool Vertical::lessThan( const Plane *plane1, const Plane *plane2)

I think this article might explain it better:
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=142