[Qt-interest] qhash - user defined key type

Jonas Gehring jonas.gehring at boolsoft.org
Thu Aug 20 23:17:31 CEST 2009


On Thursday 20 August 2009 22:30:55 Yifei Li wrote:
> Thank you for the reply.
>
> I did provide an implementation like yours. The implementation was omitted
> to make the minimal example look concise :)
>
> Anything else you can think of?
>
> Yifei

It seems like the Test namespace is causing problems. The solution is rather 
odd (found by trial & error):

	namespace Test {
 		inline uint qHash(const Foo& f) {
 			return ::qHash(f.id());
 		}
	}

Don't forget the two colons in front of qHash(int), though...

Regards,
Jonas

> ----- Original Message -----
> From: "Jonas Gehring" <jonas.gehring at boolsoft.org>
> To: qt-interest at trolltech.com
> Sent: Thursday, August 20, 2009 4:08:09 PM GMT -05:00 US/Canada Eastern
> Subject: Re: [Qt-interest] qhash - user defined key type
>
> Hi,
>
> You need to provide an implementation of qHash(const Test::Foo&), like
>
> 	inline uint qHash(const Foo& f) {
> 		return qHash(f.id());
> 	}
>
> This one is using qHash(int), as provided by Qt itself.
>
>
> Regards,
> Jonas
>
> On Thursday 20 August 2009 21:43:00 Yifei Li wrote:
> > 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
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090820/03ed37ab/attachment.bin 


More information about the Qt-interest-old mailing list