Results 1 to 5 of 5

Thread: Using custom class as a key (QSet)

  1. #1
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Using custom class as a key (QSet)

    Hello! I have a very simple class, called Node.

    Node.h
    Qt Code:
    1. #ifndef NODE_H
    2. #define NODE_H
    3.  
    4. #include <QSet>
    5.  
    6. class Node
    7. {
    8.  
    9. public:
    10. Node();
    11. Node(const uchar &c);
    12.  
    13. uchar val;
    14. QSet<Node> childNodes;
    15.  
    16. bool addChild(const Node& node);
    17.  
    18. inline bool operator==(const Node &n1, const Node &n2)
    19. {
    20. return n1.val == n2.val;
    21. }
    22.  
    23. inline uint qHash(const Node &node)
    24. {
    25. return qHash(node.val);
    26. }
    27.  
    28. };
    29.  
    30. #endif // NODE_H
    To copy to clipboard, switch view to plain text mode 
    Node.cpp
    Qt Code:
    1. #include "Node.h"
    2.  
    3. Node::Node()
    4. {
    5. val = -1;
    6. }
    7.  
    8. Node::Node(const uchar &c)
    9. {
    10. val = c;
    11. }
    12.  
    13. bool Node::addChild(const Node &node)
    14. {
    15. if(childNodes.contains(node))
    16. return false;
    17.  
    18. childNodes.insert(node);
    19. return true;
    20. }
    To copy to clipboard, switch view to plain text mode 
    I get the following error messages:

    • 'bool Node::operator==(const Node&, const Node&)' must take exactly one argument
    • no matching function for call to 'qHash(const Node&)'
    • no match for 'operator==' in key0==((QHashNode<Node, QHashDummyValue*)this)->QHashNode<Node,QHashDummyValue>::key'


    What can be the problem?

    Thanks in advance!

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using custom class as a key (QSet)

    The problem is exactly what the compiler says.

    First you want to have your == operator more or less like this:
    Qt Code:
    1. bool operator== ( const Node & other ) const
    To copy to clipboard, switch view to plain text mode 
    because you want to compare object "this" with some "other" object.

    The second problem is, I think, that qHash function should be implemented outside the class.
    Last edited by calhal; 28th April 2011 at 08:48. Reason: spelling corrections
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. The following user says thank you to calhal for this useful post:

    daujeroti (28th April 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using custom class as a key (QSet)

    Read in documentation of QSet last two paragraphs of Detailed Description.

  5. #4
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using custom class as a key (QSet)

    The second problem is, I think, that qHash function should be implemented outside the class.
    That's the point! Thank You!

  6. #5
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using custom class as a key (QSet)

    this:
    Qt Code:
    1. return qHash(node.val);
    To copy to clipboard, switch view to plain text mode 
    should be:
    Qt Code:
    1. return ::qHash(node.val);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Custom QwtPlot class
    By Carlton in forum Qwt
    Replies: 8
    Last Post: 6th September 2010, 14:44
  2. Signals not going through to custom class.
    By GunBlade in forum Qt Programming
    Replies: 3
    Last Post: 15th August 2010, 12:50
  3. QSet<QVariant> is supported ?
    By nikhilqt in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2010, 07:14
  4. QSet and custom class
    By been_1990 in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2010, 16:36
  5. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 17:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.