Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to provide a global qHash() for QSet
Forum Updated to NodeBB v4.3 + New Features

how to provide a global qHash() for QSet

Scheduled Pinned Locked Moved Solved General and Desktop
qsetqhash
4 Posts 3 Posters 2.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0odiuo00 Offline
    0odiuo00 Offline
    0odiuo0
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 :)

        1 Reply Last reply
        2
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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);
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved