PDA

View Full Version : qSort with lessThan



soul_rebel
19th August 2008, 17:04
Hi everyone!

I want to do custom sorting with a QList (its a list of pointers so the sorting has to be custom...)

I followed the doc and wrote:
bool QBsdPortGlobal::lessThan(QBsdPort* p1, QBsdPort* p2);
{
return p1->origin() < p2->origin();
} but whenver I call
qSort(m_collection.begin(),
m_collection.end(),
lessThan); I get:
bsdportglobal.cpp:497: error: no matching function for call to 'qSort(QList<QBsdPort*>::iterator, QList<QBsdPort*>::iterator, <unresolved overloaded function type>)'
/usr/local/include/qt4/QtCore/qalgorithms.h:203: note: candidates are: void qSort(RandomAccessIterator, RandomAccessIterator, LessThan) [with RandomAccessIterator = QList<QBsdPort*>::iterator, LessThan = bool (QBsdPortGlobal::*)(QBsdPort*, QBsdPort*)]

Why is lessThan() unresovled? I put it in the source and it is declared in the header. The fcuntion works otherwise...

Thanks for your help!

aamer4yu
19th August 2008, 17:40
Where are you calling the function qSort from ?
lessThan seems to be a member function of QBsdPortGlobal. So if are calling qSort outside the class, u must be getting error.
Hope am not wrong :D

soul_rebel
19th August 2008, 17:44
Thanks for the reply.

I only call qSort from other member-functions of QBsdPort::global...

caduel
19th August 2008, 17:46
You can not use a member function (needs an object to be called upon).

You can:
* make your function static
* make a plain function (outside of a class)
* make a regular function object
* use something like Boost.Bind

HTH

soul_rebel
19th August 2008, 18:14
Thanks, I made it static.