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. class operator overloading error
Forum Updated to NodeBB v4.3 + New Features

class operator overloading error

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 1.3k Views 1 Watching
  • 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.
  • J jericho63

    Hello everybody,

    I try to rewrite c++ Builder project to qt and i have a problem with the operator overloading in a class (i think it's that but not sure).

    here my class definition

    class cSoldierd
    {
    public:
        friend int  operator < ( const cSoldierd &s1, const cSoldierd &s2 )
        {
            return ( (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe) < (s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) );
        };
        friend bool operator == (  const cSoldierd &s1 , const cSoldierd &s2   )
        {
            return ((s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) == (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe));
        };
    
        QString id;
        QString nom;
        QString prenom;
        QString initiale;
        QString initiale2;
        QString suffixe;
        QString unit;
        QString sunit;
        QString asn;
    
        QString ligne()
        {
            return id+"-"+nom+"-"+prenom+"-"+initiale+"-"+initiale2+"-"+suffixe+"-"+unit+"-"+sunit+"-"+asn;
    
        }
        int nb;
    
    };
    

    I store object in a Qset, like this :

    cSoldierd temp;
        temp.asn = "123456789";
        temp.nom = "toto";
        temp.prenom = "titi";
        temp.initiale = "t";
        temp.initiale2 = "y";
        temp.suffixe = "jr";
        temp.unit = "137th";
        temp.id =  "1";
        temp.sunit = "co c";
        temp.nb = 1;
    
    
        QSet<cSoldierd> scSoldierd;
        scSoldierd.insert(temp);
    

    but i have this error :

    C:\Qt\5.12.12\mingw73_64\include\QtCore\qhashfunctions.h:118: erreur : no matching function for call to 'qHash(const cSoldierd&)'
                                               ^
    
    Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @jericho63 said in class operator overloarding error:

    function for call to 'qHash(const cSoldierd&)'

    So where did you define this function?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    2
    • J jericho63

      Hello everybody,

      I try to rewrite c++ Builder project to qt and i have a problem with the operator overloading in a class (i think it's that but not sure).

      here my class definition

      class cSoldierd
      {
      public:
          friend int  operator < ( const cSoldierd &s1, const cSoldierd &s2 )
          {
              return ( (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe) < (s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) );
          };
          friend bool operator == (  const cSoldierd &s1 , const cSoldierd &s2   )
          {
              return ((s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) == (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe));
          };
      
          QString id;
          QString nom;
          QString prenom;
          QString initiale;
          QString initiale2;
          QString suffixe;
          QString unit;
          QString sunit;
          QString asn;
      
          QString ligne()
          {
              return id+"-"+nom+"-"+prenom+"-"+initiale+"-"+initiale2+"-"+suffixe+"-"+unit+"-"+sunit+"-"+asn;
      
          }
          int nb;
      
      };
      

      I store object in a Qset, like this :

      cSoldierd temp;
          temp.asn = "123456789";
          temp.nom = "toto";
          temp.prenom = "titi";
          temp.initiale = "t";
          temp.initiale2 = "y";
          temp.suffixe = "jr";
          temp.unit = "137th";
          temp.id =  "1";
          temp.sunit = "co c";
          temp.nb = 1;
      
      
          QSet<cSoldierd> scSoldierd;
          scSoldierd.insert(temp);
      

      but i have this error :

      C:\Qt\5.12.12\mingw73_64\include\QtCore\qhashfunctions.h:118: erreur : no matching function for call to 'qHash(const cSoldierd&)'
                                                 ^
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #3

      @jericho63 said in class operator overloarding error:

      QSet<cSoldierd> scSoldierd;

      If you look at the documentation of QSet you can read:

      QSet<T> is one of Qt's generic container classes. It stores values in an unspecified order and provides very fast lookup of the values. Internally, QSet<T> is implemented as a QHash.

      • https://doc.qt.io/qt-6/qset.html#details

      And because it's unknown to Qt how to "hash" a type cSoldierd, you get this error.
      So, as @Christian-Ehrlicher said, you need to define for your custom class, what to hash.

      An example is shown in QHash documentation:

      #ifndef EMPLOYEE_H
      #define EMPLOYEE_H
      
      class Employee
      {
      public:
          Employee() {}
          Employee(const QString &name, QDate dateOfBirth);
          ...
      
      private:
          QString myName;
          QDate myDateOfBirth;
      };
      
      inline bool operator==(const Employee &e1, const Employee &e2)
      {
          return e1.name() == e2.name()
                 && e1.dateOfBirth() == e2.dateOfBirth();
      }
      
      inline size_t qHash(const Employee &key, size_t seed)
      {
          return qHashMulti(seed, key.name(), key.dateOfBirth());
      }
      
      #endif // EMPLOYEE_H
      

      Also:

      Note: In Qt 6 it is possible to define a qHash() overload taking only one argument; support for this is deprecated. Starting with Qt 7, it will be mandatory to use a two-arguments overload. If both a one-argument and a two-arguments overload are defined for a key type, the latter is used by QHash (note that you can simply define a two-arguments version, and use a default value for the seed parameter).

      • https://doc.qt.io/qt-6/qhash.html#the-hashing-function

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • J Offline
        J Offline
        jericho63
        wrote on last edited by
        #4
        This post is deleted!
        Pl45m4P 1 Reply Last reply
        0
        • J jericho63

          This post is deleted!

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #5

          @jericho63 said in class operator overloading error:

          erreur : type 'const QString' does not provide a call operator

          This says it all.
          In the example the members are private and therefore access functions are used to retrieve the value, which you don't have.
          Your nom and prenom are just (public) strings in your class, while key.name(), as used in the example, is the get function to return the private class member.
          Make your members private and write get functions or remove the (), so that it's not a function call anymore.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          J 1 Reply Last reply
          2
          • Pl45m4P Pl45m4

            @jericho63 said in class operator overloading error:

            erreur : type 'const QString' does not provide a call operator

            This says it all.
            In the example the members are private and therefore access functions are used to retrieve the value, which you don't have.
            Your nom and prenom are just (public) strings in your class, while key.name(), as used in the example, is the get function to return the private class member.
            Make your members private and write get functions or remove the (), so that it's not a function call anymore.

            J Offline
            J Offline
            jericho63
            wrote on last edited by
            #6

            thank , this is a long time i have no made code.
            I remember it now.
            i have also this error

            'qHashMulti' was not declared in this scope
            

            this error mean, i need an .h file, but i find a QMultiHash only.

            I don't understand

            Pl45m4P 1 Reply Last reply
            0
            • J jericho63

              thank , this is a long time i have no made code.
              I remember it now.
              i have also this error

              'qHashMulti' was not declared in this scope
              

              this error mean, i need an .h file, but i find a QMultiHash only.

              I don't understand

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #7

              @jericho63

              Do you use Qt6 or Qt5?

              Found out that the qHashMulti expression is only available in Qt6.0.0 and later.

              • https://doc.qt.io/qt-6/qhash.html#qHashMulti

              I guess you have to pick another qHash function from one of those, if you use Qt5:

              • https://doc.qt.io/qt-5/qhash.html#related-non-members

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              J 1 Reply Last reply
              1
              • Pl45m4P Pl45m4

                @jericho63

                Do you use Qt6 or Qt5?

                Found out that the qHashMulti expression is only available in Qt6.0.0 and later.

                • https://doc.qt.io/qt-6/qhash.html#qHashMulti

                I guess you have to pick another qHash function from one of those, if you use Qt5:

                • https://doc.qt.io/qt-5/qhash.html#related-non-members
                J Offline
                J Offline
                jericho63
                wrote on last edited by
                #8

                @Pl45m4

                i use qt6.6

                Pl45m4P 1 Reply Last reply
                0
                • J jericho63

                  @Pl45m4

                  i use qt6.6

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #9

                  @jericho63

                  Then including QHash or QMultiHash should fix this.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  J 1 Reply Last reply
                  1
                  • Pl45m4P Pl45m4

                    @jericho63

                    Then including QHash or QMultiHash should fix this.

                    J Offline
                    J Offline
                    jericho63
                    wrote on last edited by jericho63
                    #10

                    @Pl45m4
                    hello

                    ok, no morre error at the class side declaration but another one yet when i insert objet un Qset

                                cSoldierd temp;
                                temp.asn = query3.value(5).toString();
                                temp.nom = query3.value(1).toString();
                                temp.prenom = query3.value(2).toString();
                                temp.initiale = query3.value(3).toString();
                                temp.initiale2 = query3.value(4).toString();
                                temp.suffixe = query3.value(6).toString();
                                temp.unit = query3.value(7).toString();
                                temp.id =  query3.value(0).toString();
                                temp.sunit = query3.value(8).toString();
                                temp.nb = 1;
                                scSoldierd.insert(temp);
                    
                    C:\Users\john MIller\Documents\Gest_base\mainwindow.cpp:243: erreur : static assertion failed: The key type must have a qHash overload or a std::hash specialization
                    
                    
                    jsulmJ 1 Reply Last reply
                    0
                    • J jericho63

                      @Pl45m4
                      hello

                      ok, no morre error at the class side declaration but another one yet when i insert objet un Qset

                                  cSoldierd temp;
                                  temp.asn = query3.value(5).toString();
                                  temp.nom = query3.value(1).toString();
                                  temp.prenom = query3.value(2).toString();
                                  temp.initiale = query3.value(3).toString();
                                  temp.initiale2 = query3.value(4).toString();
                                  temp.suffixe = query3.value(6).toString();
                                  temp.unit = query3.value(7).toString();
                                  temp.id =  query3.value(0).toString();
                                  temp.sunit = query3.value(8).toString();
                                  temp.nb = 1;
                                  scSoldierd.insert(temp);
                      
                      C:\Users\john MIller\Documents\Gest_base\mainwindow.cpp:243: erreur : static assertion failed: The key type must have a qHash overload or a std::hash specialization
                      
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @jericho63 So, is there a qHash overload for the key type?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      J 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @jericho63 So, is there a qHash overload for the key type?

                        J Offline
                        J Offline
                        jericho63
                        wrote on last edited by
                        #12

                        @jsulm

                        I suppose not, but how i do this?
                        I have reread the doc, i haven't find an example.

                        class cSoldierd
                            {
                            public:
                                friend int  operator < ( const cSoldierd &s1, const cSoldierd &s2 )
                                {
                                    return ( (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe) < (s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) );
                                };
                                friend bool operator == (  const cSoldierd &s1 , const cSoldierd &s2   )
                                {
                                    return ((s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) == (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe));
                                };
                            
                                inline size_t qHash(const cSoldierd &key, size_t seed)
                                {
                                    return qHashMulti (seed, key.nom,key.prenom,key.initiale,key.initiale2,key.suffixe);
                            
                                }
                                
                            
                                QString id;
                                QString nom;
                                QString prenom;
                                QString initiale;
                                QString initiale2;
                                QString suffixe;
                                QString unit;
                                QString sunit;
                                QString asn;
                            
                                QString ligne()
                                {
                                    return id+"-"+nom+"-"+prenom+"-"+initiale+"-"+initiale2+"-"+suffixe+"-"+unit+"-"+sunit+"-"+asn;
                            
                                }
                                int nb;
                            
                            };
                        
                        jsulmJ 1 Reply Last reply
                        0
                        • J jericho63

                          @jsulm

                          I suppose not, but how i do this?
                          I have reread the doc, i haven't find an example.

                          class cSoldierd
                              {
                              public:
                                  friend int  operator < ( const cSoldierd &s1, const cSoldierd &s2 )
                                  {
                                      return ( (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe) < (s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) );
                                  };
                                  friend bool operator == (  const cSoldierd &s1 , const cSoldierd &s2   )
                                  {
                                      return ((s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) == (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe));
                                  };
                              
                                  inline size_t qHash(const cSoldierd &key, size_t seed)
                                  {
                                      return qHashMulti (seed, key.nom,key.prenom,key.initiale,key.initiale2,key.suffixe);
                              
                                  }
                                  
                              
                                  QString id;
                                  QString nom;
                                  QString prenom;
                                  QString initiale;
                                  QString initiale2;
                                  QString suffixe;
                                  QString unit;
                                  QString sunit;
                                  QString asn;
                              
                                  QString ligne()
                                  {
                                      return id+"-"+nom+"-"+prenom+"-"+initiale+"-"+initiale2+"-"+suffixe+"-"+unit+"-"+sunit+"-"+asn;
                              
                                  }
                                  int nb;
                              
                              };
                          
                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @jericho63 said in class operator overloading error:

                          but how i do this?

                          By defining size_t qHash(const cSoldierd &key, size_t seed) outside of the class, not as class member. Same for the other operators in your code (there is also no need to use keyword "friend" for methods).

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          J 1 Reply Last reply
                          2
                          • jsulmJ jsulm

                            @jericho63 said in class operator overloading error:

                            but how i do this?

                            By defining size_t qHash(const cSoldierd &key, size_t seed) outside of the class, not as class member. Same for the other operators in your code (there is also no need to use keyword "friend" for methods).

                            J Offline
                            J Offline
                            jericho63
                            wrote on last edited by
                            #14

                            @jsulm

                            ok, i have deplace size_t qHash(const cSoldierd &key, size_t seed) and the operator outside the class like thos :

                            class cSoldierd
                            {
                            public:
                            
                            
                            
                                QString id;
                                QString nom;
                                QString prenom;
                                QString initiale;
                                QString initiale2;
                                QString suffixe;
                                QString unit;
                                QString sunit;
                                QString asn;
                            
                                QString ligne()
                                {
                                    return id+"-"+nom+"-"+prenom+"-"+initiale+"-"+initiale2+"-"+suffixe+"-"+unit+"-"+sunit+"-"+asn;
                            
                                }
                                int nb;
                            
                            };
                            inline size_t qHash(const cSoldierd &key, size_t seed)
                            {
                                return qHashMulti (seed, key.nom,key.prenom,key.initiale,key.initiale2,key.suffixe);
                            
                            }
                            int  operator < ( const cSoldierd &s1, const cSoldierd &s2 )
                            {
                                return ( (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe) < (s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) );
                            };
                            bool operator == (  const cSoldierd &s1 , const cSoldierd &s2   )
                            {
                                return ((s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) == (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe));
                            };
                            

                            but now i have more errors:

                            :-1: erreur : CMakeFiles/Gest_base.dir/main.cpp.obj: in function `bool std::__constant_string_p<char>(char const*)':
                            
                            \mainwindow.h:52: erreur : multiple definition of `operator<(cSoldierd const&, cSoldierd const&)'; CMakeFiles/Gest_base.dir/Gest_base_autogen/mocs_compilation.cpp.obj:mainwindow.h:52: first defined here
                            

                            i don't understand the second error, i have only one definition for each operator

                            Christian EhrlicherC J 2 Replies Last reply
                            0
                            • J jericho63

                              @jsulm

                              ok, i have deplace size_t qHash(const cSoldierd &key, size_t seed) and the operator outside the class like thos :

                              class cSoldierd
                              {
                              public:
                              
                              
                              
                                  QString id;
                                  QString nom;
                                  QString prenom;
                                  QString initiale;
                                  QString initiale2;
                                  QString suffixe;
                                  QString unit;
                                  QString sunit;
                                  QString asn;
                              
                                  QString ligne()
                                  {
                                      return id+"-"+nom+"-"+prenom+"-"+initiale+"-"+initiale2+"-"+suffixe+"-"+unit+"-"+sunit+"-"+asn;
                              
                                  }
                                  int nb;
                              
                              };
                              inline size_t qHash(const cSoldierd &key, size_t seed)
                              {
                                  return qHashMulti (seed, key.nom,key.prenom,key.initiale,key.initiale2,key.suffixe);
                              
                              }
                              int  operator < ( const cSoldierd &s1, const cSoldierd &s2 )
                              {
                                  return ( (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe) < (s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) );
                              };
                              bool operator == (  const cSoldierd &s1 , const cSoldierd &s2   )
                              {
                                  return ((s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) == (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe));
                              };
                              

                              but now i have more errors:

                              :-1: erreur : CMakeFiles/Gest_base.dir/main.cpp.obj: in function `bool std::__constant_string_p<char>(char const*)':
                              
                              \mainwindow.h:52: erreur : multiple definition of `operator<(cSoldierd const&, cSoldierd const&)'; CMakeFiles/Gest_base.dir/Gest_base_autogen/mocs_compilation.cpp.obj:mainwindow.h:52: first defined here
                              

                              i don't understand the second error, i have only one definition for each operator

                              Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              You must define them inline

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              3
                              • J jericho63

                                @jsulm

                                ok, i have deplace size_t qHash(const cSoldierd &key, size_t seed) and the operator outside the class like thos :

                                class cSoldierd
                                {
                                public:
                                
                                
                                
                                    QString id;
                                    QString nom;
                                    QString prenom;
                                    QString initiale;
                                    QString initiale2;
                                    QString suffixe;
                                    QString unit;
                                    QString sunit;
                                    QString asn;
                                
                                    QString ligne()
                                    {
                                        return id+"-"+nom+"-"+prenom+"-"+initiale+"-"+initiale2+"-"+suffixe+"-"+unit+"-"+sunit+"-"+asn;
                                
                                    }
                                    int nb;
                                
                                };
                                inline size_t qHash(const cSoldierd &key, size_t seed)
                                {
                                    return qHashMulti (seed, key.nom,key.prenom,key.initiale,key.initiale2,key.suffixe);
                                
                                }
                                int  operator < ( const cSoldierd &s1, const cSoldierd &s2 )
                                {
                                    return ( (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe) < (s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) );
                                };
                                bool operator == (  const cSoldierd &s1 , const cSoldierd &s2   )
                                {
                                    return ((s2.nom+"|"+s2.prenom+"|"+s2.initiale+"|"+s2.initiale2+"|"+s2.suffixe) == (s1.nom+"|"+s1.prenom+"|"+s1.initiale+"|"+s1.initiale2+"|"+s1.suffixe));
                                };
                                

                                but now i have more errors:

                                :-1: erreur : CMakeFiles/Gest_base.dir/main.cpp.obj: in function `bool std::__constant_string_p<char>(char const*)':
                                
                                \mainwindow.h:52: erreur : multiple definition of `operator<(cSoldierd const&, cSoldierd const&)'; CMakeFiles/Gest_base.dir/Gest_base_autogen/mocs_compilation.cpp.obj:mainwindow.h:52: first defined here
                                

                                i don't understand the second error, i have only one definition for each operator

                                J Offline
                                J Offline
                                jericho63
                                wrote on last edited by
                                #16

                                @jericho63

                                that's work!
                                Thank you
                                The operator overload no longer need to be define in a class now?

                                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