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.2k 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.
  • P Pl45m4
    8 Feb 2024, 18:32

    @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 8 Feb 2024, 19:38 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

    P 1 Reply Last reply 8 Feb 2024, 20:29
    0
    • J jericho63
      8 Feb 2024, 19:38

      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

      P Offline
      P Offline
      Pl45m4
      wrote on 8 Feb 2024, 20:29 last edited by Pl45m4 2 Aug 2024, 20:30
      #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 8 Feb 2024, 20:46
      1
      • P Pl45m4
        8 Feb 2024, 20:29

        @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 8 Feb 2024, 20:46 last edited by
        #8

        @Pl45m4

        i use qt6.6

        P 1 Reply Last reply 9 Feb 2024, 03:35
        0
        • J jericho63
          8 Feb 2024, 20:46

          @Pl45m4

          i use qt6.6

          P Offline
          P Offline
          Pl45m4
          wrote on 9 Feb 2024, 03:35 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 9 Feb 2024, 08:28
          1
          • P Pl45m4
            9 Feb 2024, 03:35

            @jericho63

            Then including QHash or QMultiHash should fix this.

            J Offline
            J Offline
            jericho63
            wrote on 9 Feb 2024, 08:28 last edited by jericho63 2 Sept 2024, 08:29
            #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
            
            
            J 1 Reply Last reply 9 Feb 2024, 08:31
            0
            • J jericho63
              9 Feb 2024, 08:28

              @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
              
              
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 9 Feb 2024, 08:31 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 9 Feb 2024, 10:01
              0
              • J jsulm
                9 Feb 2024, 08:31

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

                J Offline
                J Offline
                jericho63
                wrote on 9 Feb 2024, 10:01 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;
                    
                    };
                
                J 1 Reply Last reply 9 Feb 2024, 10:05
                0
                • J jericho63
                  9 Feb 2024, 10:01

                  @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;
                      
                      };
                  
                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 9 Feb 2024, 10:05 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 9 Feb 2024, 10:23
                  2
                  • J jsulm
                    9 Feb 2024, 10:05

                    @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 9 Feb 2024, 10:23 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

                    C J 2 Replies Last reply 9 Feb 2024, 10:29
                    0
                    • J jericho63
                      9 Feb 2024, 10:23

                      @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

                      C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 9 Feb 2024, 10:29 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
                        9 Feb 2024, 10:23

                        @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 9 Feb 2024, 13:38 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

                        15/16

                        9 Feb 2024, 10:29

                        • Login

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