gpt4 book ai didi

c++ - 无法创建以用户定义类为键的二维 unordered_map

转载 作者:行者123 更新时间:2023-11-28 00:10:04 30 4
gpt4 key购买 nike

如标题所述,在使用以用户定义的类作为键的二维 map 时,我无法让 VC++ 编译器工作。这是我要声明的 map :

std::unordered_map<tag, std::map<tag, std::pair<bool, bool>>> transitRights;

这里是类标签的声明和 std::hash 函数的特化:

class tag
{
public:
tag();
tag(const char[3]);
tag(const std::string&);
void operator=(const tag&);
const bool operator==(const tag&)const;
operator const std::string()const;
const char& getA()const { return a; }
const char& getB()const { return b; }
const char& getC()const { return c; }
private:
char a, b, c;
};


namespace std
{
template<> class hash<tag>
{
public:
size_t operator()(const tag& _tag) const
{
return hash<char>()(_tag.getA()) ^ hash<char>()(_tag.getB()) ^ hash<char>()(_tag.getC());
}
};
};

尝试编译时,Visual Studio 向我抛出一堆这些神秘的错误消息:

1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(600): error C2664: 'std::pair<const _Kty,_Ty>::pair(const std::pair<const _Kty,_Ty> &)' : cannot convert argument 1 from 'const tag' to 'const std::pair<const _Kty,_Ty> &'
1> with
1> [
1> _Kty=int
1> , _Ty=std::pair<bool,bool>
1> ]
1> Reason: cannot convert from 'const tag' to 'const std::pair<const _Kty,_Ty>'
1> with
1> [
1> _Kty=int
1> , _Ty=std::pair<bool,bool>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,const tag&>(_Objty *,const tag &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const int,std::pair<bool,bool>>,void *>
1> , _Objty=std::pair<const int,std::pair<bool,bool>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,const tag&>(_Objty *,const tag &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const int,std::pair<bool,bool>>,void *>
1> , _Objty=std::pair<const int,std::pair<bool,bool>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(872) : see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,const tag&>(std::allocator<_Other> &,_Objty *,const tag &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Tree_node<std::pair<const int,std::pair<bool,bool>>,void *>>
1> , _Ty=std::pair<const int,std::pair<bool,bool>>
1> , _Other=std::_Tree_node<std::pair<const int,std::pair<bool,bool>>,void *>
1> , _Objty=std::pair<const int,std::pair<bool,bool>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(872) : see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,const tag&>(std::allocator<_Other> &,_Objty *,const tag &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Tree_node<std::pair<const int,std::pair<bool,bool>>,void *>>
1> , _Ty=std::pair<const int,std::pair<bool,bool>>
1> , _Other=std::_Tree_node<std::pair<const int,std::pair<bool,bool>>,void *>
1> , _Objty=std::pair<const int,std::pair<bool,bool>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree(933) : see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,const tag&>(_Ty *,const tag &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const int,std::pair<bool,bool>>,void *>
1> , _Ty=std::pair<const int,std::pair<bool,bool>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree(933) : see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,const tag&>(_Ty *,const tag &)' being compiled
1> with
1> [
1> _Other=std::_Tree_node<std::pair<const int,std::pair<bool,bool>>,void *>
1> , _Ty=std::pair<const int,std::pair<bool,bool>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree(1176) : see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_buy<std::pair<const _Kty,_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::_Buynode<const tag&>(const tag &)' being compiled
1> with
1> [
1> _Kty=int
1> , _Ty=std::pair<bool,bool>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree(1176) : see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_buy<std::pair<const _Kty,_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::_Buynode<const tag&>(const tag &)' being compiled
1> with
1> [
1> _Kty=int
1> , _Ty=std::pair<bool,bool>
1> ]
1> nation.cpp(600) : see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<const tag&>(const tag &)' being compiled
1> with
1> [
1> _Kty=int
1> , _Ty=std::pair<bool,bool>
1> , _Pr=std::less<int>
1> , _Alloc=std::allocator<std::pair<const int,std::pair<bool,bool>>>
1> ]
1> nation.cpp(600) : see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<const tag&>(const tag &)' being compiled
1> with
1> [
1> _Kty=int
1> , _Ty=std::pair<bool,bool>
1> , _Pr=std::less<int>
1> , _Alloc=std::allocator<std::pair<const int,std::pair<bool,bool>>>
1> ]

如果能深入了解我在这里做错了什么,我们将不胜感激。

最佳答案

问题在于嵌套对象是 std::map(不是 unordered_map),为此您需要对 tag 进行小于比较

但是,您应该能够构建 unordered_mapunordered_map

关于c++ - 无法创建以用户定义类为键的二维 unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617918/

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