Why does no operator< exist for two QStrings?
-
Background: I get a compile error in the following code line:
return m_Id.operator< (other.m_Id);
Both m_Id and other.m_Id are of type QString.
Compiler output (MinGW) is:error C2664: 'bool QString::operator <(const QByteArray &) const': cannot convert argument 1 from 'const QString' to 'QLatin1String'
That made me scratch my head. Looking up help, I realized that only the following operator< overloads are defined for QString:
bool operator<(QLatin1String other) const bool operator<(const char *other) const bool operator<(const QByteArray &other) const
...as well as this global function:
bool operator<(const char *s1, const QString &s2)
Why is that? And what's the best way to fix it?
-
Background: I get a compile error in the following code line:
return m_Id.operator< (other.m_Id);
Both m_Id and other.m_Id are of type QString.
Compiler output (MinGW) is:error C2664: 'bool QString::operator <(const QByteArray &) const': cannot convert argument 1 from 'const QString' to 'QLatin1String'
That made me scratch my head. Looking up help, I realized that only the following operator< overloads are defined for QString:
bool operator<(QLatin1String other) const bool operator<(const char *other) const bool operator<(const QByteArray &other) const
...as well as this global function:
bool operator<(const char *s1, const QString &s2)
Why is that? And what's the best way to fix it?
Hi
I cant guess reason for the design choices but others have discussed it
http://stackoverflow.com/questions/16281014/operator-for-qstring -
Background: I get a compile error in the following code line:
return m_Id.operator< (other.m_Id);
Both m_Id and other.m_Id are of type QString.
Compiler output (MinGW) is:error C2664: 'bool QString::operator <(const QByteArray &) const': cannot convert argument 1 from 'const QString' to 'QLatin1String'
That made me scratch my head. Looking up help, I realized that only the following operator< overloads are defined for QString:
bool operator<(QLatin1String other) const bool operator<(const char *other) const bool operator<(const QByteArray &other) const
...as well as this global function:
bool operator<(const char *s1, const QString &s2)
Why is that? And what's the best way to fix it?
@Asperamanca
i dont understand.
you need to sort Arraylist or Vector?bool comperator(QString a, QString b){
return a < b;
}Qsort(array.begin(),array.end(),comperator);
-
Hi
I cant guess reason for the design choices but others have discussed it
http://stackoverflow.com/questions/16281014/operator-for-qstring@mrjj said in Why does no operator< exist for two QStrings?:
Hi
I cant guess reason for the design choices but others have discussed it
http://stackoverflow.com/questions/16281014/operator-for-qstringI think you are sitting on a different boat...I was referring to operator< (less than)
-
@Asperamanca
i dont understand.
you need to sort Arraylist or Vector?bool comperator(QString a, QString b){
return a < b;
}Qsort(array.begin(),array.end(),comperator);
@Taz742 said in Why does no operator< exist for two QStrings?:
@Asperamanca
i dont understand.
you need to sort Arraylist or Vector?bool comperator(QString a, QString b){
return a < b;
}Qsort(array.begin(),array.end(),comperator);
That gave me the hint. The code it not from me, and I didn't see the obvious: By writing
return m_Id.operator< (other.m_Id);
instead of
return m_Id < other.m_Id;
the code explicitly calls one of the overloads in QString, instead of any globally defined function.