[Qt-interest] qhash - user defined key type

xiaolei Shang xiaolei.shang at esrtechnology.com
Fri Aug 21 17:22:38 CEST 2009


Hi:

You need put declaration of operator == and qHash() in the definition of 
class Foo:
class Foo
{
public:
   Foo(id);
 ...

inline bool operator==(const Foo&f2)
inline uint qHash(const Foo& f);

...
};

Cheers

Xiaolei
> Here is the problem I'm having with QHash right now ( Below is a minimal example that will reproduce my problem)
>
> /** Header file from some third-party library */
>
> namespace Test {
> class Foo{
> public:
>    Foo(id);
>    int id() const { return m_id; }
> private:
>    int m_id;
> };
> }
>
> /** My header file */
>
> using namespace Test;
>
> class Bar{
> public:
>    void init() 
>    {
>        Foo f(1);
>        indicators[f] = false;
>    }
> private:
>    QHash<Foo, bool>  indicators;
> };
>
> //  overload operators so that Foo can be used as a key type
> inline bool operator==(const Foo& f1, const Foo&f2)
> inline uint qHash(const Foo& f);
>
> /** main.cpp */
> int main 
> {
>     Bar b;
>     b.init();
>     return 0;
> }
>
> /** Failed to compile the above code */
> ERROR: no matching function for call to ‘qHash(const Test::Foo&)’
>
> What should I do to get rid of the error? thanks
>
>
> Yifei
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>   




More information about the Qt-interest-old mailing list