如何解决自定义类对象的unordered_set hasher函数中的此错误?

Nothing_8484

我第一次使用无序映射,并使用自定义对象。我试图编写以下功能。有一些错误需要我帮助。

这是课程。

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

};

以下是自定义函数:

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

我收到以下错误:

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;
^~~~~~~~ 

错误列表要长得多,我认为这足以使某人理解该错误。我为自定义功能介绍了该页面:http : //thispointer.com/how-to-use-unordered_set-with-user-defined-classes-tutorial-example/有什么建议么?谢谢阅读。

编辑1:Unordered_set创建:

unordered_set<Node,NodeHasher,NodeComparator> openList;

编辑2:比较运算符中的错误。

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

在这种情况下,编译器可以推断出T和U是什么。一种解决方案是删除模板和硬代码int,因为您知道自己正在使用一对int。

而且我相信您放错了const说明符-应该将其放在声明的末尾

或者,如果要使NodeHasher适用于的不同类型pair,则可以使其成为模板类。

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创建为

unordered_set<Node, NodeHasher<int,int>, NodeComparator> us; //T and U are int

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

访问unordered_set中的自定义对象

为什么带有自定义哈希函数和自定义类的unordered_set需要初始数量的存储桶?

如何通过传递命名函数为unordered_set显式指定自定义哈希函数?

具有自定义谓词的std :: unordered_set中的未定义行为

如何解决 react-redux 中的此错误“错误:操作必须是普通对象。使用自定义中间件进行异步操作。”

如何解决:错误:动作必须是普通对象。使用自定义中间件进行异步操作。

如何解决JPA自定义存储库中的错误“ SQL语法错误”

尝试在自定义C#窗口类中实现WinApi时,如何解决Windows错误1407(找不到窗口类)?

如何解决自定义DataGrid中控件命名错误?

如何解决自定义反应组件中 createContext 的打字稿错误

如何解决用户自定义类型未定义的错误?

如何解决此错误.....我正在尝试实现自定义Firebase通知应用程序

如何从自定义类的列表中获取对象?

此查询中的错误:如何解决?

在MiniZinc中如何解决此错误?

如何解决 redux 错误 使用自定义中间件进行异步操作?

如何解决此currying函数中的类型不匹配错误?

如何解决此“预期在箭头函数中返回值”错误

在Keras中使用自定义步骤激活功能会导致“'tuple'对象没有属性'_keras_shape'”错误。如何解决呢?

如何在自定义类中使用 boost::unordered_set?

调用自定义Spark UDF时如何解决Java中的NoSuchMethodException

如何解决VS代码中的自定义标签帮助程序问题?

如何解决 JSR-303 自定义验证器中的依赖关系

如何解决Outlook中自定义表格标签的不兼容问题

自定义路径组件中如何解决道具传播被禁止?

如何解决此特定代码行的“应用程序定义的错误”或“对象定义的错误”?

如何解决“错误:未为类‘对象’定义运算符‘[]’?”

如何解决Azure Active Directory B2C自定义属性中的验证错误

如何解决此CodeIgniter的“函数实参过多”错误?