PDA

View Full Version : qSort() problem



darksaga
16th August 2007, 19:21
hi all,

I have a strange prob to sort my QList with the qSort() function.

the class to be sorted looks like:


#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass{
public:
MyClass(){}
~MyClass{}
bool operator<(const MyClass& a) const {
return getBpCount() < a.getBpCount();
}
const float getBpCount(void) const{return /*some calculated float*/;}
}
#endif


in the class, where I use the class above, I do the following:


QList<MyClass*> *list = new QList<MyClass*>;
for(){/*create new MyClasses & add to list*/}

//finally
//checked the listorder here1
qSort(*list);
//and here2


the list should be sorted, but it isn't :confused:

there is some change between here1 & here2 but @ here2 the list isn't oredered in an ascending or descending order, it seems to be random to me

can anybody tell me what I'm missing or doing wrong???

regards

jpn
16th August 2007, 20:43
The list gets sorted by pointer values. You should store objects (eg. QList<MyClass>) to get it sorted like you want.

jpn
16th August 2007, 21:05
Oh, and by the way. Actually you can sort pointers too with the help of custom comparator function:


bool myClassLessThan(const MyClass* a, const MyClass* b)
{
return a->getBpCount() < b->getBpCount();
}

qSort(list->begin(), list->end(), myClassLessThan);


As a side note, QList is an implicitly shared class (http://doc.trolltech.com/4.3/shared.html) so usually it's allocated on stack. ;)

darksaga
17th August 2007, 05:05
ok,
this explains the "random" order after using the qSort funcion :rolleyes:

did it the way u suggested, and tried to use the lessThan function, but now my compiler starts to bitch around :confused:

if the lessThan function is defined in MyClass, the compiler says:


error c2065: 'myClassLessThan': unknown identifier


if I declare lessThan in the class, where i use the qSort function, it says:


moc_ResultContainer.cpp
..\..\qt4\include\QtCore\qalgorithms.h(359) : error C2064: Ausdruck ergibt keine Funktion, die 2 Argumente übernimmt
..\..\qt4\include\QtCore\qalgorithms.h(184): Siehe Verweis auf Instanziierung der kompilierten Funktionsvorlage 'void QAlgorithmsPrivate::qSortHelper<RandomAccessIterator,T,LessThan>(RandomAccessIterator,RandomAccessIterator,const T & ,LessThan)'
with
[
RandomAccessIterator=QList<MyClass *>::iterator,
T=MyClass *,
LessThan=bool (__thiscall ResultContainer::* )(const MyClass *,const MyClass *)
]
c:\devel\projects\projekt_main_vs\release\../src\Workbench\ResultContainer.h(56): Siehe Verweis auf Instanziierung der kompilierten Funktionsvorlage 'void qSort<QList<T>::iterator,bool(__thiscall ResultContainer::* )(const MyClass *,const MyClass *)>(RandomAccessIterator,RandomAccessIterator,LessTha n)'
with
[
T=MyClass *,
RandomAccessIterator=QList<MyClass *>::iterator,
LessThan=bool (__thiscall ResultContainer::* )(const MyClass *,const MyClass *)
]
..\..\qt4\include\QtCore\qalgorithms.h(364) : error C2064: Ausdruck ergibt keine Funktion, die 2 Argumente übernimmt
..\..\qt4\include\QtCore\qalgorithms.h(366) : error C2064: Ausdruck ergibt keine Funktion, die 2 Argumente übernimmt
..\..\qt4\include\QtCore\qalgorithms.h(374) : error C2064: Ausdruck ergibt keine Funktion, die 2 Argumente übernimmt


so, what is it now, that I'm missing?

jpn
17th August 2007, 09:58
You are passing a function pointer so the function must be

either declared as static member function
or declared in the global namespace (not a member function)

For more details, see C++ FAQ LITE: [33] Pointers to member functions (http://www.parashift.com/c++-faq-lite/pointers-to-members.html).

darksaga
17th August 2007, 12:47
You are passing a function pointer so the function must be

either declared as static member function
or declared in the global namespace (not a member function)

For more details, see C++ FAQ LITE: [33] Pointers to member functions (http://www.parashift.com/c++-faq-lite/pointers-to-members.html).
makes sense :o, though I don't like static & global declared stuff...

works like a charm now, thanks alot jpn ;)