C++ 如何解决自定义类对象的无序散列函数中的此错误?

C++ 如何解决自定义类对象的无序散列函数中的此错误?,c++,c++11,c++14,hashset,unordered-set,C++,C++11,C++14,Hashset,Unordered Set,我第一次使用无序贴图,并且使用自定义对象。我试着写下面的函数。有些错误我需要帮助 这是一节课 class Node { public: int g= 0, h=0; char val; //Char value in the grid pair<int,int> pos,parent; bool par_prsnt = fa

我第一次使用无序贴图,并且使用自定义对象。我试着写下面的函数。有些错误我需要帮助

这是一节课

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

};
错误列表要长得多,我认为这可能足以让人理解错误。我参考了此页面以获取自定义函数:。有什么建议吗?谢谢你的阅读

编辑1: 无序集创建:

unordered_set<Node,NodeHasher,NodeComparator> openList;
并将无序的_集创建为

unordered_set<Node, NodeHasher<int,int>, NodeComparator> us; //T and U are int
无序设置我们//T和U是int

为什么要将
运算符()模板化?
?请阅读一些答案,使用std::pair比较对象时,我需要模板来组合第一个值和第二个值。我认为您误解了这个答案。编译器无法推导出哈希函数中的
T
U
。既然您知道它们将是
int
或者将
NanoHasher
作为模板类,而不是将其
操作符()作为模板,那么为什么不使用硬编码
int
呢这几乎解决了所有的值错误,谢谢,正确地放置了“const”并硬编码为“int”。但是,hasher函数的运算符()的比较运算符中仍然存在一些错误。添加编辑。你看过这个演示吗?--它编译时没有任何错误,我唯一更改的是NanoHasher部分。也许,main中还有其他代码?对,错误不在hasher函数中。这是另一个代码。
unordered_set<Node,NodeHasher,NodeComparator> openList;
Player 1: compilation error
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_equals_val::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator >; _Value = const Node]’:
/usr/include/c++/7/bits/stl_algo.h:120:14: required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val]’
/usr/include/c++/7/bits/stl_algo.h:161:23: required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val]’
/usr/include/c++/7/bits/stl_algo.h:3907:28: required from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator >; _Tp = Node]’
solution.cc:147:73: required from here
/usr/include/c++/7/bits/predefined_ops.h:241:17: error: no match for ‘operator==’ (operand types are ‘Node’ and ‘const Node’)
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/stl_iterator.h:859:5: note: candidate: template bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator&, const __gnu_cxx::__normal_iterator&)
operator==(const __normal_iterator& __lhs,
^~~~~~~~
/usr/include/c++/7/bits/stl_iterator.h:859:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/predefined_ops.h:241:17: note: ‘Node’ is not derived from ‘const __gnu_cxx::__normal_iterator’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/stl_iterator.h:866:5: note: candidate: template bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator&, const __gnu_cxx::__normal_iterator&)
operator==(const __normal_iterator& __lhs,
^~~~~~~~
/usr/include/c++/7/bits/stl_iterator.h:866:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/predefined_ops.h:241:17: note: ‘Node’ is not derived from ‘const __gnu_cxx::__normal_iterator’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/vector:61,
solution.cc:3:
/usr/include/c++/7/ext/new_allocator.h:155:5: note: candidate: template bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator&, const __gnu_cxx::new_allocator&)
operator==(const new_allocator&, const new_allocator&)
^~~~~~~~
/usr/include/c++/7/ext/new_allocator.h:155:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/predefined_ops.h:241:17: note: ‘Node’ is not derived from ‘const __gnu_cxx::new_allocator’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
solution.cc: In function ‘std::vector aStar(std::vector >&, std::pair, std::pair)’:
solution.cc:173:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
cc1plus: some warnings being treated as errors
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) ;
  }
};
template <typename T, typename U>
struct NodeHasher
{
    size_t operator()(const Node &obj) const
    {
        pair<T, U> position = obj.get_pos();
        return 3* std::hash<T>()(position.first) + std::hash<U>()(position.second) ;
    }
};
unordered_set<Node, NodeHasher<int,int>, NodeComparator> us; //T and U are int