问题 无序的一对对,编译错误


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

到目前为止我有:

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

我将在未来创建一套Move,现在我只有:

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

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

`/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/


12155
2018-01-22 16:21


起源

您使用的操作系统和编译器版本是什么? - Adrian
与您的问题无关,但您返回一个指向局部变量的指针。 - Abhishek Bansal
MacOS mavericks和gcc。配置为: - prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-dir = / usr / include / c ++ / 4.2.1 Apple LLVM 5.0(clang-500.2.79) (基于LLVM 3.3svn)目标:x86_64-apple-darwin13.0.0线程模型:posix - user3223763
我猜它没有为你的对定义哈希函数。返回局部变量的地址的问题当然会引起你的注意。 - CashCow
我已将您的标题更新为更清晰。将C ++语言称为“Cpp”令人困惑;该缩写通常是指C预处理器。我本来只是将“Cpp”更改为“C ++”,但该信息已经在标签中。 - Keith Thompson


答案:


如果您没有专业化,则会显示该错误消息 std::hash 或为您的无序容器提供一个哈希类型(参见例如 在Visual C ++和clang中使用C ++ 11 unordered_set)。在这种情况下,XCode错误特别不友好!

C ++ 11不为对(或元组)提供散列,即使是可散列类型也是如此。 这个讨论 表明这主要是由于没有时间让任何更好的东西;但是我不知道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;

看到 如何在C ++ 0x中组合哈希值? 如何编写哈希函数。

或者,你可以做 Move 一个用户定义的类(可能是一个好主意!),然后专业化 std::hash 会没事的。


11
2018-01-22 16:33



我很想写 struct PairHasher 这对任何一对透明地适用于我自己(打电话 hash<X> 对于每一半的一半)。当我在它的时候,去做吧 struct TupleHasher 支持对和元组。 - Yakk - Adam Nevraumont