View Full Version : How to implement operator<() for QMap
learning_qt
8th January 2009, 13:18
I hope to use QMap as template
QMap<ClassA, ClassB> localMap;
class ClassA
{
public:
setIndex(int index);
setFlag(bool flag);
checkIndex();
checkFlag();
private:
int index;
bool flag;
};
In order to use "Key" in QMap, I have to implement operator<(). But I do not know how to do it. Please help me:
}
jpn
8th January 2009, 13:26
There is an example in QMap docs.
learning_qt
9th January 2009, 06:35
I implemented by referring to the example in QMap doc. but it does not work properly. When I try to access the elements in QMap template, there were still some errors caused by operator<()
caduel
9th January 2009, 07:07
so, why don't you show use how you tried to implement a "less than" operation for ClassA?
learning_qt
9th January 2009, 07:40
void ClassA::setKey(int key)
{
index = key;
}
void ClassA::setFlag(bool status)
{
flag= status;
}
int ClassA::checkKey() const
{
return index;
}
bool ClassA::checkFlag() const
{
return flag;
}
bool ClassA::operator<(const ClassA &i1)
{
if (index != i1.checkKey())
return index < i1.chekcKey();
return false;
}
wysota
9th January 2009, 08:28
I'd say the operator has to be const.
caduel
9th January 2009, 10:21
And even if it would not have to be: it should be const.
Besides, that function is equivalent to:
bool ClassA::operator<(const ClassA &i1) const
{
return index < i1.index;
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.