c++ - 一元 'operator' 没有在 C++ 中定义

标签 c++

unary 'operator' : 'type' does not define this operator or a conversion to a type acceptable to the predefined operator

在c++中使用TriMesh::VertexHandle作为map的键值时出现问题。

map<TriMesh::VertexHandle, TriMesh::VertexHandle> intvmap;
    for (vector<TriMesh::VertexHandle>::iterator it = oldVertices.begin(); it != oldVertices.end(); ++it){

        bool isInternal = mesh.property(vIsInternal, *it);
        if (isInternal) {
            TriMesh::Point pos = mesh.point(*it);
            TriMesh::VertexHandle mirror = mesh.add_vertex(pos - z * 2 * mesh.property(vHeight, *it));
            mesh.property(vHeight, mirror) = -mesh.property(vHeight, *it);
            mesh.property(vIsInternal, mirror) = true;
            intvmap.insert((*it), mirror);
        }
    }

insert() 不工作并得到上面的错误。

    template<class _Iter>
    void insert(_Iter _First, _Iter _Last)
    {   // insert [_First, _Last) one at a time
    _DEBUG_RANGE(_First, _Last);
    for (; _First != _Last; ++_First)

        emplace_hint(end(), *_First);
    }

我认为是operator++的问题,所以我在头文件中添加了代码

TriMesh::VertexHandle& operator++(TriMesh::VertexHandle& vh){ //++A
    vh.__increment();
    return vh;
}
TriMesh::VertexHandle operator++(TriMesh::VertexHandle & p_oRight, int) // A++
{
    TriMesh::VertexHandle & copy = p_oRight;
    copy.__increment();
    return copy;
}

但是,错误依然存在。 我想知道是否有解决方案。

最佳答案

当您插入 std::map 时按照您的预期方式,您应该插入 std::pair<key_type, value_type> ,不是键作为一个参数,而是值作为第二个参数。

这里有两种方式调用map::insert :

intvmap.insert(std::make_pair(*it, mirror));

或者使用大括号初始化器:

intvmap.insert({*it, mirror});

关于c++ - 一元 'operator' 没有在 C++ 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52902149/

相关文章:

c++ - boost 绑定(bind)模板错误

c++ - 将逗号分隔的文本文件读入数组

c++ - 如何合并、拆分和查询第 k 个排序列表?

c++ - Chrome 中的 NPAPI tcp http 服务器 c++

c++ - Std 或 boost atomic unsigned char[32]

c++ - 为什么参数修饰符(即 'const' 或 'volatile' )不被视为函数类型或签名的一部分?

c++ - const、指针、typedef 和星号

c++11 使用 std::fill 填充二维动态分配数组

c++ - 如何编写特征转换以将 'const' 添加到类型*

c++:键为映射的映射