title: Setting a custom class to be used with unordered set - element can't be found in the set
body: <p>What I am trying to do is to make it possible to use std::unordered_set with my custom class Vector2 - <strong>including the possibility to search for objects of such class that are already in a set</strong>.</p>
  
  <p>Let me give more details. The header of my class Vector2, including the custom hash table, is the following:</p>
  
  <pre><code>Class Vector2
  {
  private:
      static int lastID;
      const int id;
      int x;
      int y;
  
  public:
      Vector2(int _x, int _y);
      ~Vector2();
  
      bool Vector2::operator==(const Vector2&amp; other) const;
  
      int getId() const;
      int getX() const;
      int getY() const;
  };
  
  namespace std
  {
      template&lt;&gt;
      struct hash&lt;Vector2&gt;
      {
          size_t
              operator()(const Vector2&amp; obj) const
          {
              return hash&lt;int&gt;()(obj.getId());
          }
      };
  }
  </code></pre>
  
  <p>The implementation of the memeber functions of such class is trivial:</p>
  
  <pre><code>int Vector2::lastID = 0;
  
  Vector2::Vector2(int _x, int _y) : id(lastID++)
  {
      x = _x;
      y = _y;
  }
  
  int Vector2::getId() const
  {
      return id;
  }
  
  int Vector2::getX() const
  {
      return x;
  }
  
  int Vector2::getY() const
  {
      return y;
  }
  
  bool Vector2::operator==(const Vector2&amp; other) const
  {
      if (x != other.x || y != other.y) return false;
      return true;
  }
  </code></pre>
  
  <p>Then, my main functions looks like the following:</p>
  
  <pre><code>std::unordered_set&lt;Vector2&gt; mySet;
  mySet.insert(Vector2(1, 2));
  mySet.insert(Vector2(3, 11));
  mySet.insert(Vector2(-5, 0));
  
  Vector2 whatToLookFor(1, 2);
  if (mySet.find(whatToLookFor) != mySet.end())
  {
      std::cout &lt;&lt; "Found it!" &lt;&lt; std::endl;
  }
  else
  {
      std::cout &lt;&lt; "Nothing found." &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>However, while I would expect the output to be <code>Found it!</code>, it is actually <code>Nothing found</code>. It means, while the Vector2 objects <code>Vector2(1, 2)</code>, <code>Vector2(3, 11)</code> and <code>Vector2(-5, 0)</code> are inserted in <code>mySet</code>, they later are not found when searching inside such set.</p>
  
  <p>What am I doing wrong?</p>