[Qt-interest] qhash - user defined key type

Eric Clark eclark at ara.com
Fri Aug 21 19:54:11 CEST 2009


The reason for the inline is a C++ thing. Not a Qt thing. Anytime you place the code for a function in the header file, along with the declaration, you must precede the definition with the keyword inline because the function is truly inlined. If the declaration was in the header and the definition was in a cpp file, you would not need the inline keyword.

Eric

-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Yifei Li
Sent: Friday, August 21, 2009 12:38 PM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] qhash - user defined key type

I finally found out the reason why the following solution did not work for my real code:

in my real code, I missed the keyword 'inline' before uint qHash

so I ended up getting linking errors like  " multiple definition of `Test::qHash(Test::Foo const&)' "

Now my question becomes why is 'inline' necessary in this situation?

Yifei
----- Original Message -----
From: "Jonas Gehring" <jonas.gehring at boolsoft.org>
To: qt-interest at trolltech.com
Sent: Thursday, August 20, 2009 5:17:31 PM GMT -05:00 US/Canada Eastern
Subject: Re: [Qt-interest] qhash - user defined key type

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


_______________________________________________
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




More information about the Qt-interest-old mailing list