Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does no operator< exist for two QStrings?
QtWS25 Last Chance

Why does no operator< exist for two QStrings?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 965 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by Asperamanca
    #1

    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?

    mrjjM Taz742T 2 Replies Last reply
    0
    • A Asperamanca

      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?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      I cant guess reason for the design choices but others have discussed it
      http://stackoverflow.com/questions/16281014/operator-for-qstring

      A 1 Reply Last reply
      0
      • A Asperamanca

        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?

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by
        #3

        @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);

        Do what you want.

        A 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          I cant guess reason for the design choices but others have discussed it
          http://stackoverflow.com/questions/16281014/operator-for-qstring

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @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-qstring

          I think you are sitting on a different boat...I was referring to operator< (less than)

          1 Reply Last reply
          0
          • Taz742T Taz742

            @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);

            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved