dimanche 29 avril 2018

How to solve this error in unordered_set hasher function of custom class objects?

I am using unordered map for the first time and that with custom objects. I tried to write the below functions. There are errors that I need help with.

This is the class.

class Node
{
  public:
    int g= 0, h=0;
    char val;                                                              //Char value in the grid
    pair<int,int> pos,parent;  
    bool par_prsnt = false;                                                //Bool to check if the parent is set


    Node(pair<int,int>nodePos,char value)
    {
    pos=nodePos;
    val=value;
    }  

    int move_cost(Node other)
    {
    if (val=='.')
        return 0;
    else
        return 1;
    }

    pair<int,int> get_pos() const
    {
    return pos;
    }

    void set_parent(pair<int,int> par)
    {
    parent = par;
    par_prsnt = true;
    }

};

Below is the custom functions:

// Custom Hasher for Class Node, to be used for Unordered_set
struct NodeHasher
{
  template <typename T, typename U>
  size_t
  const operator()(const Node &obj)
  {
    pair<T,U> position;
    position = obj.get_pos();
    return 3* std::hash<T>()(position.first) + std::hash<U>()(position.second) ;
  }
};

// Custom Comparator for Class Node, to be used for Unordered_set
struct NodeComparator
{
  bool
  const operator()(const Node  &obj1, const  Node  &obj2) const
  {
    if (obj1.get_pos() == obj2.get_pos())
      return true;
    return false;
  }
};

I get the following errors:

Player 1: compilation error
In file included from /usr/include/c++/7/bits/hashtable.h:35:0,
from /usr/include/c++/7/unordered_map:47,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:117,
solution.cc:7:
/usr/include/c++/7/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash’:
/usr/include/c++/7/type_traits:143:12: required from ‘struct std::__and_,  std::__detail::__is_noexcept_hash >’
/usr/include/c++/7/type_traits:154:31: required from ‘struct std::__not_, std::__detail::__is_noexcept_hash > >’
/usr/include/c++/7/bits/unordered_set.h:98:63: required from ‘class std::unordered_set’
solution.cc:119:51: required from here
/usr/include/c++/7/bits/hashtable_policy.h:87:34: error: no match for call to ‘(const NodeHasher) (const Node&)’
noexcept(declval()(declval()))>
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
solution.cc:83:9: note: candidate: template const size_t NodeHasher::operator()(const Node&)
const operator()(const Node &obj)
^~~~~~~~
solution.cc:83:9: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/bits/hashtable.h:35:0,
from /usr/include/c++/7/unordered_map:47,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:117,
solution.cc:7:
/usr/include/c++/7/bits/hashtable_policy.h:87:34: note: couldn't deduce template parameter ‘T’
noexcept(declval()(declval()))>
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/stl_pair.h:59,
from /usr/include/c++/7/bits/stl_algobase.h:64,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/type_traits: In instantiation of ‘struct std::__not_, std::__detail::__is_noexcept_hash > >’:
/usr/include/c++/7/bits/unordered_set.h:98:63: required from ‘class std::unordered_set’
solution.cc:119:51: required from here
/usr/include/c++/7/type_traits:154:31: error: ‘value’ is not a member of ‘std::__and_, std::__detail::__is_noexcept_hash >’
: public __bool_constant
^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/unordered_set:48:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:118,
solution.cc:7:
/usr/include/c++/7/bits/unordered_set.h: In instantiation of ‘class std::unordered_set’:
solution.cc:119:51: required from here
/usr/include/c++/7/bits/unordered_set.h:98:63: error: ‘value’ is not a member of ‘std::__not_, std::__detail::__is_noexcept_hash > >’
typedef __uset_hashtable _Hashtable;
^~~~~~~~~~
/usr/include/c++/7/bits/unordered_set.h:105:45: error: ‘value’ is not a member of ‘std::__not_, std::__detail::__is_noexcept_hash > >’
typedef typename _Hashtable::key_type key_type;
^~~~~~~~ 

The error list is much longer, I think this much maybe enough for someone to understand the error. I referred this page for the custom functions : http://thispointer.com/how-to-use-unordered_set-with-user-defined-classes-tutorial-example/. Any suggestions? Thanks for reading.

Aucun commentaire:

Enregistrer un commentaire