class operator overloading error
-
wrote on 8 Feb 2024, 10:54 last edited by aha_1980 2 Aug 2024, 12:57
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&)' ^
-
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&)' ^
@jericho63 said in class operator overloarding error:
function for call to 'qHash(const cSoldierd&)'
So where did you define this function?
-
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&)' ^
wrote on 8 Feb 2024, 12:40 last edited by@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.
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).
-
wrote on 8 Feb 2024, 18:01 last edited byThis post is deleted!
-
wrote on 8 Feb 2024, 18:32 last edited by Pl45m4 2 Aug 2024, 18:56
@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.
Yournom
andprenom
are just (public) strings in your class, whilekey.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. -
@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.
Yournom
andprenom
are just (public) strings in your class, whilekey.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.wrote on 8 Feb 2024, 19:38 last edited bythank , 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
-
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
wrote on 8 Feb 2024, 20:29 last edited by Pl45m4 2 Aug 2024, 20:30Do you use Qt6 or Qt5?
Found out that the
qHashMulti
expression is only available in Qt6.0.0 and later.I guess you have to pick another
qHash
function from one of those, if you use Qt5: -
Do you use Qt6 or Qt5?
Found out that the
qHashMulti
expression is only available in Qt6.0.0 and later.I guess you have to pick another
qHash
function from one of those, if you use Qt5: -
wrote on 9 Feb 2024, 03:35 last edited by
Then including
QHash
orQMultiHash
should fix this. -
Then including
QHash
orQMultiHash
should fix this.wrote on 9 Feb 2024, 08:28 last edited by jericho63 2 Sept 2024, 08:29@Pl45m4
hellook, 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
-
@Pl45m4
hellook, 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
@jericho63 So, is there a qHash overload for the key type?
-
@jericho63 So, is there a qHash overload for the key type?
wrote on 9 Feb 2024, 10:01 last edited byI 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; };
-
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; };
@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).
-
@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).
wrote on 9 Feb 2024, 10:23 last edited byok, 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
-
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
You must define them
inline
-
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
wrote on 9 Feb 2024, 13:38 last edited bythat's work!
Thank you
The operator overload no longer need to be define in a class now?
1/16