断言失败,列出迭代器而不是dereferencable

时间:2012-12-13 02:11:35

标签: c++

我在以下代码中从此行获得断言失败“list iterator not dereferencable”:

iter2 = iter-> vertex_list.erase(iter2);

我不确定为什么会这样,我确保列表不是空的。完整代码如下:

void Map_Quad_List::Vertex_List_Duplicate_Removal() {
    for (std::list<Map_Polygon>::iterator iter = map_polygon_list.begin(); iter != map_polygon_list.end(); iter++) { //iterate through polygons
        std::vector<XMFLOAT3> position_list; //list of vertices already tried
            for (std::list<Vertex>::iterator iter2 = iter->vertex_list.begin(); iter2 != iter->vertex_list.end(); iter2++ ) {//iterate through vertices
                bool dup = false;
                for (std::vector<XMFLOAT3>::iterator pos_list_iter = position_list.begin(); pos_list_iter != position_list.end(); pos_list_iter++) { //check vertex against position_list
                    if ((!position_list.empty()) && (XMFLOAT3_Comparison(*pos_list_iter, iter2->position))) { //if vertex already exists in position_list
                        iter2 = iter->vertex_list.erase(iter2); //remove vertex from polygon's vertex_list
                        dup = true;
                    }
                }
                if (dup == false) //if there is not a duplicate in position_list, add it to position_list
                position_list.push_back(iter2->position);
            }

    }
}

map_polygon_list是类类型Map_Polygon

的std ::列表

Map_Polygon类有一个类型为Vertex的std :: list

Vertex是一个由XMFLOAT3(位置)和XMFLOAT4(颜色)组成的类,此函数仅处理位置(XMFLOAT3)

XMFLOAT3_Comparison是一个比较两个XMFLOAT3的函数,如果它们是相同的则返回true

1 个答案:

答案 0 :(得分:0)

看起来iter2可能会转到最内部for循环中的iter->vertex_list.end(),这会导致此断言,因为指向的项为null。