无序对集合,编译错误

13

我正在尝试创建一个无序的pair集合。

到目前为止,我有:

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

未来我会创建一组动作,目前只有:

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/

你使用的是哪个操作系统和编译器版本? - Adrian
7
跟你的问题无关,但你正在返回一个指向局部变量的指针。 - 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
2
我猜测你的pair并没有定义哈希函数。返回本地变量的地址问题当然会在后面困扰你。 - CashCow
1
我已经更新了您的标题,使其更清晰。将C++语言称为"Cpp"是令人困惑的;这个缩写通常指的是C预处理器。我本来会只将"Cpp"更改为"C++",但这个信息已经在标签中了。 - Keith Thompson
显示剩余2条评论
1个回答

12

如果您未对unordered容器进行特化std::hash或为其提供哈希程序,就会出现该错误消息(例如,请参见在Visual C++和Clang中使用C++11 unordered_set)。在这种情况下,XCode错误信息尤为不友好!

C++11没有为对可哈希类型的pair(或tuple)提供哈希函数。 此讨论表明,这主要是由于没有足够的时间得到更好的结果;但是我不知道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即可。


我会考虑编写 struct PairHasher,使其能够透明地处理任何一对(对每个半对调用 hash<X>)。顺便说一下,我还会将其制作成支持对和元组的 struct TupleHasher - Yakk - Adam Nevraumont

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接