我有一个带有“float2, unsigned int”对的 vector 模板,例如:
std::vector<std::pair<float2, unsigned int>> myVec;
然后我尝试将这样的一对添加到 vector 中:
unsigned int j = 0;
float2 ab = {1.0, 2.0};
myVec.push_back(std::make_pair(ab, j));
这是我期望的工作方式,但当我尝试编译它时出现错误:
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(163): error C2536: 'std::_Pair_base<_Ty1,_Ty2>::std::_Pair_base<_Ty1,_Ty2>::first' : cannot specify explicit initializer for arrays
1> with
1> [
1> _Ty1=float2 ,
1> _Ty2=unsigned int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(166) : see declaration of 'std::_Pair_base<_Ty1,_Ty2>::first'
1> with
1> [
1> _Ty1=float2 ,
1> _Ty2=unsigned int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(247) : see reference to function template instantiation 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base<float(&)[2],unsigned int&>(_Other1,_Other2)' being compiled
1> with
1> [
1> _Ty1=float2 ,
1> _Ty2=unsigned int,
1> _Other1=float (&)[2],
1> _Other2=unsigned int &
1> ]
1> myTest.cpp(257) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<float2(&),unsigned int&>(_Other1,_Other2)' being compiled
1> with
1> [
1> _Ty1=float2,
1> _Ty2=unsigned int,
1> _Other1=float2 (&),
1> _Other2=unsigned int &
1> ]**strong text**
将此数据类型添加到我的对保持 vector 中的正确方法是什么?
float2 类型定义为:
typedef float float2[2];
最佳答案
C++ 数组几乎在每次使用时都会衰减为指针。更改 float2
:
typedef std::array<float, 2> float2;
或者,如果您还没有 C++11,您可以使用 boost::array
。
关于c++ - std::make_pair with float array (float2, unsigned int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18814929/