简体   繁体   中英

Iterator dereferencing assertion error

I'm using an iterator to loop through list's and then dereferencing them into a obj or array of the same time, I am doing this in 2 different instances but I am getting an assertion error saying I cannot dereference the iterator.

I'm confused because in one of the functions it works fine, however in the second function it's throwing the error but they have been coded the same way.

the function the assertion error is being thrown:

Coord backTrack(){ // recalls to intersection where dead-end route was found
    list<Coord>::iterator it = pathHistory.end();
    Coord deadEnd = *it;
    coordsToUnmark.push_back(deadEnd);
    pathHistory.pop_back();
    return pathHistory.front();
}

in this function it works fine:

int findPath (Maze& theMaze, const Coord& start, const Coord& end, Coord path[]){
    patherFinder(theMaze, start.x, start.y, end);
    list<Coord>::iterator it;
    int ii = 0;
    for (it = pathHistory.begin(); it != pathHistory.end(); it++){
        path[ii] = *it;
        ii++;
}

阅读注释后,我意识到list.end()并不是列表容器中past-the-end元素的最后一个元素,这解释了为什么我无法取消引用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM