无法创建unordered_map的元组< int,int,int> [英] Cannot create unordered_map of tuple<int, int, int>

查看:155
本文介绍了无法创建unordered_map的元组< int,int,int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 std :: unordered_map< std :: tuple< int,int,int> float> 。但是,VS2010不会编译实例化。我明确地提供了 hash 特殊化和运算符== 重载。错误消息指的是

  std :: unordered_map< NodeType,float> g_score; 

这里是错误的全文:

  1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\tuple(127):error C2440:'initializing'不能从'const NodeType'转换为'int'
1>没有可以执行此转换的用户定义转换运算符,或者不能调用运算符
1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\xxtuple0(9):参见函数模板实例化的参考std :: tr1 :: _ Cons_node< _Car,_Cdr> :: _ Cons_node< ; _Ty,std :: tr1 :: _ Nil&,std :: tr1 :: _Nil&,std :: tr1 :: _Nil&,std :: tr1 :: _Nil& ;,std :: tr1 :: _Nil& :tr1 :: _Nil&,std :: tr1 :: _Nil&,std :: tr1 :: _Nil&>(_Farg0&&,_Farg1,_Farg2,_Farg3,_Farg4, _Farg5,_Farg6,_Farg7,_Farg8,_Farg9)'
1> with
1> [
1> _Car = int,
1> _Cdr = std :: tr1 :: _ Cons_node< int,std :: tr1 :: _ Cons_node< int,std :: tr1 :: _Tuple_type< std :: tr1 :: _ Nil,std :: tr1 :: _Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _Nil,std :: tr1 :: _ Nil> :: _ Type>>,
1> _Ty = NodeType,
1> _Farg0 = NodeType,
1> _Farg1 = std :: tr1 :: _ Nil&
1> _Farg2 = std :: tr1 :: _ Nil&,
1> _Farg3 = std :: tr1 :: _ Nil&,
1> _Farg4 = std :: tr1 :: _ Nil&,
1> _Farg5 = std :: tr1 :: _ Nil&,
1> _Farg6 = std :: tr1 :: _ Nil&,
1> _Farg7 = std :: tr1 :: _ Nil&,
1> _Farg8 = std :: tr1 :: _ Nil&,
1> _Farg9 = std :: tr1 :: _ Nil&
1> ]
1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\utility(145):参见函数模板实例化'std :: tr1 :: tuple< _Arg0,_Arg1,_Arg2> ;: :正在编译的tuple< const std :: tr1 :: tuple< _Arg0,_Arg1,_Arg2>>(_Farg0&& with
1> [
1> _Arg0 = int,
1> _Arg1 = int,
1> _Arg2 = int,
1> _Farg0 = const std :: tr1 :: tuple< int,int,int>
1> ]
1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\utility(142):编译类模板成员函数'std :: _ Pair_base< _Ty1,_Ty2> :: Pair_base :: tr1 :: tuple <_Arg0,_Arg1,_Arg2>&&&,float&&&)'
1> with
1> [
1> _Ty1 = const NodeType,
1> _Ty2 = float,
1> _Arg0 = int,
1> _Arg1 = int,
1> _Arg2 = int
1> ]
1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\utility(174):参见类模板实例化'std :: _ Pair_base< _Ty1,_Ty2& b 1> with
1> [
1> _Ty1 = const NodeType,
1> _Ty2 = float
1> ]
1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\unordered_map(279):参见类模板实例化'std :: pair< _Ty1,_Ty2& b 1> with
1> [
1> _Ty1 = const NodeType,
1> _Ty2 = float
1> ]
1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\unordered_map(275):在编译类模板成员函数float& std :: tr1 :: unordered_map< _Kty,_Ty& :: operator [](const std :: tr1 :: tuple< _Arg0,_Arg1,_Arg2>&)'
1> with
1> [
1> _Kty = NodeType,
1> _Ty = float,
1> _Arg0 = int,
1> _Arg1 = int,
1> _Arg2 = int
1> ]
1> c:\repo\render\render\sim\simcontext.cpp(155):参考类模板实例化'std :: tr1 :: unordered_map< _Kty,_Ty>'正在编译
1> ; with
1> [
1> _Kty = NodeType,
1> _Ty = float
1> ]
1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\tuple(127):错误C2439:'std :: tr1 :: _ Cons_node< _Car,_Cdr> ; :: _ Value':无法初始化成员
1> with
1> [
1> _Car = int,
1> _Cdr = std :: tr1 :: _ Cons_node< int,std :: tr1 :: _ Cons_node< int,std :: tr1 :: _Tuple_type< std :: tr1 :: _ Nil,std :: tr1 :: _Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _Nil,std :: tr1 :: _ Nil> :: _ Type>>
1> ]
1> c:\program files(x86)\microsoft visual studio 10.0 \vc\include\tuple(170):请参见声明std :: tr1 :: _ Cons_node< _Car,_Cdr> :: _ Value'
1> with
1> [
1> _Car = int,
1> _Cdr = std :: tr1 :: _ Cons_node< int,std :: tr1 :: _ Cons_node< int,std :: tr1 :: _Tuple_type< std :: tr1 :: _ Nil,std :: tr1 :: _Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _ Nil,std :: tr1 :: _Nil,std :: tr1 :: _ Nil> :: _ Type>>
1> ]

不确定要继续处理,因为我从未看到此错误。任何建议?



编辑:有趣的是, unordered_set< NodeType>

尝试使用SSCCE:

  #include< deque> 
#include< tuple>
#include< unordered_map>
#include< unordered_set>

void GetPath(){
typedef std :: tuple< int,int,int> NodeType;
struct node_hasher:public std :: unary_function< const NodeType& std :: size_t> {
std :: size_t operator()(const NodeType& node)const {
return std :: hash< int>()(std :: get< 0>(node)+ std :: get ; 0>(节点)+ std :: get< 0>(节点)+ std :: get 1(node)+ std :: get 1 );
}
};
std :: unordered_set< NodeType,node_hasher> closed_set;
std :: unordered_map< NodeType,float,node_hasher> g_score;
std :: unordered_map< NodeType,float,node_hasher> f_score;
std :: unordered_map< NodeType,NodeType,node_hasher>来自;
auto node_comparator = [&](NodeType lhs,NodeType rhs){
return f_score [lhs] f_score [rhs];
};
}

int main(){
}

无法编译在空项目中出现相同的错误。如果你取消注释 node_comparator lambda,它工作正常。难怪我遇到了麻烦 - 这是另一个VS错误。

解决方案

这是Visual C ++ 2010标准库中的一个错误。请参阅以下关于Microsoft Connect的报告:


C ++ map< tuple< ...>,V> p>

Visual Studio 11的错误已修复。您的示例使用Visual Studio 11测试版无错误地编译。


I'm using std::unordered_map<std::tuple<int, int, int>, float>. However, VS2010 will not compile the instantiation. I explicitly provided a hash specialization and an operator== overload. The error message refers to

std::unordered_map<NodeType, float> g_score;

And here is the full text of the error:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(127): error C2440: 'initializing' : cannot convert from 'const NodeType' to 'int'
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 10.0\vc\include\xxtuple0(9) : see reference to function template instantiation 'std::tr1::_Cons_node<_Car,_Cdr>::_Cons_node<_Ty,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&>(_Farg0 &&,_Farg1,_Farg2,_Farg3,_Farg4,_Farg5,_Farg6,_Farg7,_Farg8,_Farg9)' being compiled
1>          with
1>          [
1>              _Car=int,
1>              _Cdr=std::tr1::_Cons_node<int,std::tr1::_Cons_node<int,std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type>>,
1>              _Ty=NodeType,
1>              _Farg0=NodeType,
1>              _Farg1=std::tr1::_Nil &,
1>              _Farg2=std::tr1::_Nil &,
1>              _Farg3=std::tr1::_Nil &,
1>              _Farg4=std::tr1::_Nil &,
1>              _Farg5=std::tr1::_Nil &,
1>              _Farg6=std::tr1::_Nil &,
1>              _Farg7=std::tr1::_Nil &,
1>              _Farg8=std::tr1::_Nil &,
1>              _Farg9=std::tr1::_Nil &
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(145) : see reference to function template instantiation 'std::tr1::tuple<_Arg0,_Arg1,_Arg2>::tuple<const std::tr1::tuple<_Arg0,_Arg1,_Arg2>>(_Farg0 &&)' being compiled
1>          with
1>          [
1>              _Arg0=int,
1>              _Arg1=int,
1>              _Arg2=int,
1>              _Farg0=const std::tr1::tuple<int,int,int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(142) : while compiling class template member function 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base(const std::tr1::tuple<_Arg0,_Arg1,_Arg2> &&,float &&)'
1>          with
1>          [
1>              _Ty1=const NodeType,
1>              _Ty2=float,
1>              _Arg0=int,
1>              _Arg1=int,
1>              _Arg2=int
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(174) : see reference to class template instantiation 'std::_Pair_base<_Ty1,_Ty2>' being compiled
1>          with
1>          [
1>              _Ty1=const NodeType,
1>              _Ty2=float
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\unordered_map(279) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1>          with
1>          [
1>              _Ty1=const NodeType,
1>              _Ty2=float
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\unordered_map(275) : while compiling class template member function 'float &std::tr1::unordered_map<_Kty,_Ty>::operator [](const std::tr1::tuple<_Arg0,_Arg1,_Arg2> &)'
1>          with
1>          [
1>              _Kty=NodeType,
1>              _Ty=float,
1>              _Arg0=int,
1>              _Arg1=int,
1>              _Arg2=int
1>          ]
1>          c:\repo\render\render\sim\simcontext.cpp(155) : see reference to class template instantiation 'std::tr1::unordered_map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=NodeType,
1>              _Ty=float
1>          ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(127): error C2439: 'std::tr1::_Cons_node<_Car,_Cdr>::_Value' : member could not be initialized
1>          with
1>          [
1>              _Car=int,
1>              _Cdr=std::tr1::_Cons_node<int,std::tr1::_Cons_node<int,std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(170) : see declaration of 'std::tr1::_Cons_node<_Car,_Cdr>::_Value'
1>          with
1>          [
1>              _Car=int,
1>              _Cdr=std::tr1::_Cons_node<int,std::tr1::_Cons_node<int,std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type>>
1>          ]

Uncertain where to proceed, as I've never seen this error before. Any suggestions?

Edit: Interestingly, the unordered_set<NodeType> right before this compiles cleanly.

Try this for an SSCCE:

#include <deque>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

void GetPath() {
    typedef std::tuple<int, int, int> NodeType;
    struct node_hasher : public std::unary_function<const NodeType&, std::size_t> {
        std::size_t operator()(const NodeType& node) const {
            return std::hash<int>()(std::get<0>(node) + std::get<0>(node) + std::get<0>(node) + std::get<1>(node) + std::get<1>(node) + std::get<2>(node));
        }
    };
    std::unordered_set<NodeType, node_hasher> closed_set;
    std::unordered_map<NodeType, float, node_hasher> g_score;
    std::unordered_map<NodeType, float, node_hasher> f_score;
    std::unordered_map<NodeType, NodeType, node_hasher> came_from;
    auto node_comparator = [&](NodeType lhs, NodeType rhs) {
        return f_score[lhs] < f_score[rhs];
    };
}

int main() {
}

Fails to compile with the same error in the empty project. If you uncomment the node_comparator lambda, it works just fine. No wonder I had trouble with it- it's another VS bug.

解决方案

This is a bug in the Visual C++ 2010 Standard Library. See the following report on Microsoft Connect:

C++ map<tuple<...>, V> compilation problems

The bug has been fixed for Visual Studio 11. Your example compiles without error using the Visual Studio 11 Beta.

这篇关于无法创建unordered_map的元组&lt; int,int,int&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆