链接方法时转换类型错误

时间:2019-03-26 22:03:33

标签: c++ c++11

我对这段涉及迭代遍历结构RouteInfo中包含的列表的C ++代码迷失了。代码如何显示此编译错误:Severity Code Description Project File Line Suppression State Error (active) E0312 no suitable user-defined conversion from "std::_List_const_iterator<std::_List_val<std::conditional_t<true, std::_List_simple_types<int>, std::_List_iter_types<int, size_t, ptrdiff_t, int *, const int *, int &, const int &, std::_List_node<int, void *> *>>>>" to "std::_List_iterator<std::_List_val<std::conditional_t<true, std::_List_simple_types<int>, std::_List_iter_types<int, size_t, ptrdiff_t, int *, const int *, int &, const int &, std::_List_node<int, void *> *>>>>" ...

问题代码

void iterateOverListInStruct(std::multimap<RouteInfo, int> aMap)
{
    std::multimap<RouteInfo, int /* vehicle id */>::iterator it = aMap.begin();
    for (; it != aMap.end(); it++)
    {
        std::list<int>::iterator itr = it->first.secList.begin();
    }
}

并且此代码不显示任何编译器错误?

void iterateOverListInStruct(std::multimap<RouteInfo, int> aMap)
{
    std::multimap<RouteInfo, int /* vehicle id */>::iterator it = aMap.begin();
    for (; it != aMap.end(); it++)
    {
        std::list<int> list = it->first.secList;
        std::list<int>::iteartor itr = list.begin();
    }
}

这是课程RouteInfo

class RouteInfo{
    public:
        list<int>secList;}

1 个答案:

答案 0 :(得分:3)

value_type的{​​{1}}为std::multimap,因此std::pair<const Key, T>it->first.secList,并且其上的const std::list<int>返回begin() < / p>

第二个示例将其复制到新的非常量std::list<int>::const_iterator中,然后std::list<int>返回的迭代器类型为begin()

要修复错误更改,请std::list<int>::iterator类型更改为itr