gpt4 book ai didi

c++ - 无序的对集,编译错误

转载 作者:可可西里 更新时间:2023-11-01 15:14:36 25 4
gpt4 key购买 nike

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

到目前为止我有:

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

我将在未来创建一组 Moves,现在我只有:

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/

最佳答案

当您无法专门化时会出现该错误消息 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会好的。

关于c++ - 无序的对集,编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com