C++ 地图插入在VS 2015中导致C2664错误,在VS 2013中有效

C++ 地图插入在VS 2015中导致C2664错误,在VS 2013中有效,c++,c2664,C++,C2664,这段代码在VS 2013中运行得很好,但我不得不更新到VS 2015,现在它抛出了一个错误 我确实读了很多书,在谷歌上搜索了不少,但我仍然不知道如何解决这个问题 我正在使用eigen数学库做一些3d数学的东西。Eigen的Vector3d类不能用作容器的键,所以我创建了自己的Vector3dLite类来解决这个问题 class Vector3dLite { public: float VertX, VertY,VertZ; Vector3dLite(Vector3d& InputVert

这段代码在VS 2013中运行得很好,但我不得不更新到VS 2015,现在它抛出了一个错误

我确实读了很多书,在谷歌上搜索了不少,但我仍然不知道如何解决这个问题

我正在使用eigen数学库做一些3d数学的东西。Eigen的Vector3d类不能用作容器的键,所以我创建了自己的Vector3dLite类来解决这个问题

class Vector3dLite
{
public:
float VertX, VertY,VertZ;
Vector3dLite(Vector3d& InputVert)
{
    VertX = static_cast<float>(InputVert.x());
    VertY = static_cast<float>(InputVert.y());
    VertZ = static_cast<float>(InputVert.z());
}

Vector3dLite(Vector3dLite& InputVert)
{
    VertX = InputVert.VertX;
    VertY = InputVert.VertY;
    VertZ = InputVert.VertZ;
}
//more operator overloading stuff below
}
类向量3dlite
{
公众:
浮动顶点X、顶点Y、顶点Z;
Vector3dLite(Vector3d和InputVert)
{
VertX=静态_转换(InputVert.x());
VertY=静态_转换(InputVert.y());
VertZ=静态_转换(InputVert.z());
}
Vector3dLite(Vector3dLite和InputVert)
{
VertX=输入VERT.VertX;
VertY=InputVert.VertY;
VertZ=输入VERT.VertZ;
}
//下面有更多运算符重载的内容
}
这里是编译器抛出错误的地方

map<Vector3dLite, int>  VertexIds;
int unique_vertid = 0;
VertexIds.insert(make_pair(Vector3dLite(tri.Vert1), unique_vertid)); //This line
// Vert1 is an eigen Vector3d object
//...
映射顶点;
int unique_vertid=0;
插入(生成一对(Vector3dLite(tri.Vert1),唯一的vertid))//这条线
//Vert1是特征向量3D对象
//...
以下是编译器错误:

error C2664: cannot convert argument 1 from 'std::pair<Vector3dLite,int>' to 'std::pair<const _Kty,_Ty> &&'
      with
      [
          _Kty=Vector3dLite,
          _Ty=int,
          _Pr=std::less<Vector3dLite>,
          _Alloc=std::allocator<std::pair<const Vector3dLite,int>>
      ]
      and
      [
          _Kty=Vector3dLite,
          _Ty=int
      ]
错误C2664:无法将参数1从'std::pair'转换为'std::pair&'
具有
[
_Kty=矢量3dLite,
_Ty=int,
_Pr=标准::更少,
_Alloc=std::分配器
]
和
[
_Kty=矢量3dLite,
_Ty=int
]
我确实尝试在Vector3dLite对象之前编写const,但显然语法不正确


插入(生成一对(const Vector3dLite(tri.Vert1),唯一的vertid))

由于映射的值类型将const object作为第一个元素(映射键),因此通常不能使用
make\u pair
来构造值,因为推断的类型不是const

可以创建具有显式类型的对:

std::pair<const Vector3dLite, int>(Vector3dLite(tri.Vert1), unique_vertid)

另一个注意事项:构造函数应该通过
const&

获取参数。另一种选择是:只需使用
emplace
:类似于
VertexIds.emplace(tri.Vert1,unique\u vertid)的方法在适当的位置构造一对参数
std::map<Vector3dLite, int>::value_type(Vector3dLite(tri.Vert1), unique_vertid)
const Vector3dLite mapkey(tri.Vert1);
make_pair(mapkey, unique_vertid);