无序的一对对,编译错误

时间:2014-01-22 16:21:30

标签: c++

我正在尝试创建一组无序的对

到目前为止,我有:

typedef std::pair<int, int> Move;
typedef std::unordered_set<Move> Set;

我将来会创建一组Move,我现在只有:

Set* King::possibleMoves() 
{
  Set hello;    <-------- THINK ERROR OCCURS HERE
  return &hello;
}

但我一直得到这三个错误:

`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:770:38: error: 
  implicit instantiation of undefined template 'std::__1::hash<std::__1::pair<int, int>
  >'
: public integral_constant<bool, __is_empty(_Tp)> {};
                                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1951:40: note: 
  in instantiation of template class
  'std::__1::is_empty<std::__1::hash<std::__1::pair<int, int> > >' requested here
                            bool = is_empty<_T2>::value
                                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1973:44: note: 
  in instantiation of default argument for '__libcpp_compressed_pair_switch<unsigned
  long, std::__1::hash<std::__1::pair<int, int> >, false, false>' required here
template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:2357:15: note: 
  in instantiation of default argument for '__libcpp_compressed_pair_imp<unsigned long,
  std::__1::hash<std::__1::pair<int, int> > >' required here
: private __libcpp_compressed_pair_imp<_T1, _T2>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__hash_table:527:55: note: 
  in instantiation of template class 'std::__1::__compressed_pair<unsigned long,
  std::__1::hash<std::__1::pair<int, int> > >' requested here
__compressed_pair<size_type, hasher>              __p2_;
                                                  ^
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/unordered_set:330:13: note: 
  in instantiation of template class 'std::__1::__hash_table<std::__1::pair<int, int>,
  std::__1::hash<std::__1::pair<int, int> >, std::__1::equal_to<std::__1::pair<int, int>
  >, std::__1::allocator<std::__1::pair<int, int> > >' requested here
__table __table_;
        ^
King.cpp:9:7: note: in instantiation of template class
  'std::__1::unordered_set<std::__1::pair<int, int>, std::__1::hash<std::__1::pair<int,
  int> >, std::__1::equal_to<std::__1::pair<int, int> >,
  std::__1::allocator<std::__1::pair<int, int> > >' requested here
 Set hello;
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:3081:29: note: 
      template is declared here
template <class _Tp> struct hash;
                        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1951:55: error: 
  no member named 'value' in 'std::__1::is_empty<std::__1::hash<std::__1::pair<int, int>
  > >'
                            bool = is_empty<_T2>::value
                                   ~~~~~~~~~~~~~~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1973:44: note: 
  in instantiation of default argument for '__libcpp_compressed_pair_switch<unsigned
  long, std::__1::hash<std::__1::pair<int, int> >, false, false>' required here
template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

整个错误在这里(不允许我粘贴在上面)

http://fixee.org/paste/528pvoq/

1 个答案:

答案 0 :(得分:11)

如果您无法专门化std::hash或为无序容器提供哈希类型,则会显示该错误消息(请参阅例如Using C++11 unordered_set in Visual C++ and clang)。在这种情况下,XCode错误特别不友好!

C ++ 11不为对(或元组)提供散列,即使是可散列类型也是如此。 This discussion表明这主要是因为没有时间让任何更好的事情发生;但是我不知道C ++ 14中是否会有更好的东西。

专业化std::hash<std::pair<int, int>>可能不是一个好主意(并且语言不允许;专门的std模板仅允许用户定义类型),因此您必须提供一个哈希:

struct MoveHasher {
    std::size_t operator()(const std::pair<int, int> &val) const { ... }
};
typedef std::unordered_set<Move, MoveHasher> Set;

有关如何编写散列函数,请参阅How do I combine hash values in C++0x?

或者,您可以使Move成为用户定义的类(可能是个好主意!)然后专门化std::hash就可以了。