how to provide a global qHash() for QSet



  • I would like to create a QSet to contain unassignable value.
    Thus, I must provide operator==(), and a global qHash() function.But there is something wrong with my qHash().

    here is my code:

    entity.h

    struct CVexOnSec
    {
    	CVexOnSec(QVector3D P){ pos = P; }
    
    	QVector3D pos;
    
    	bool operator == (const CVexOnSec &vex) const
    	{
    		return (pos == vex.pos);
    	}
    };
    

    entity.cpp

    inline uint qHash(const CVexOnSec &t, uint seed)
    {
    	return qHash(t.pos.x());
    }
    

    and the error is error C2665.
    located in the code in qhash.h:

    template<typename T> inline uint qHash(const T &t, uint seed)
        Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(t)))
    { return (qHash(t) ^ seed); }
    

    what's wrong with my qHash() , and how can I implement it correctly?



  • did you #include <QHash> in entity.cpp? also, I'd declare uint qHash(const CVexOnSec &t, uint seed) in entity.h


  • Qt Champions 2016

    Hi
    in the sample
    http://doc.qt.io/qt-5/qhash.html#qhash

    it uses 2 parameters for the global qHash
    return qHash(key.name(), seed)

    and
    template<typename T> inline uint qHash(const T &t, uint seed)
    seem to want 2.

    So my guess u need a seed :)



  • to clarify @mrjj solution, which is 100% right

    inline uint qHash(const CVexOnSec &t, uint seed)
    {
        return qHash(t.pos.x());
    }
    

    should become

    inline uint qHash(const CVexOnSec &t, uint seed)
    {
        return qHash(t.pos.x(),seed);
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.