[Interest] QMap operator<() overload.

Koehne Kai Kai.Koehne at theqtcompany.com
Wed Feb 25 11:16:43 CET 2015



> -----Original Message-----
> From: interest-bounces+kai.koehne=theqtcompany.com at qt-project.org
> [mailto:interest-bounces+kai.koehne=theqtcompany.com at qt-project.org]
> On Behalf Of manish sharma
> Sent: Wednesday, February 25, 2015 10:51 AM
> To: interest at qt-project.org
> Subject: [Interest] QMap operator<() overload.
> 
> Hi,
> 
> I am using a QMap to maintain my sorted list based with QString as key.
> 
> QMap <QString, MyValue> list;
> 
> I have certain pattern inside key of above map and want to have my own
> sorting order than default QString operator<().
> 
> So i have following function in my cpp.
> 
> bool operator<(const QString &s1, const QString &s2) { // my logic of sorting.
> }
> 
> which leads to a following warning:
> 
> .\main.cpp(20) : warning C4273: 'operator <' : inconsistent dll linkage
>         C:\TekSDK\include\QtCore/qstring.h(585) : see previous definition of '<'
> 
> 
> What is harm with this warning, does it has any implication?

It means that there's already an operator<(const QString&, const QString&) defined. My best guess is that it's not defined which one will be used ...

>  How do to fix
> this?

You have a couple of options:

- Wrap your QString in a custom type, and use that one instead of QString
- Use std::map, which allows you to provide a custom operator<
- Use another structure entirely (e.g. QPair), and sort when you need it [1]

[1]: Might be a good idea anyway, because using QMap/std::map is very slow in a lot of use cases where you might think it's the 'right' abstraction.

Regards

Kai



More information about the Interest mailing list