矢量下标超出范围&标准c ++库超出范围C ++

时间:2017-01-22 05:20:15

标签: c++ vector

void pathfind(const int* tiles, int startTile, int endTile, int height) {//const int* tiles is my map
int currentTile = startTile; //The spawn tile



    std::vector<int> points{ startTile };
    int i;//for loops
    bool possible = true;//to make sure they 



    if (tiles[currentTile + 1] <= 2 && floor(currentTile / height) == floor(currentTile - 1 / height)) {//Less than  2 because 3 and up are non-traverseable tiles.
        for (i = 1; i <= points.size(); i++) {

            if (points[i] == currentTile + 1) {
                possible = false;
            }

        }
        if (possible == true) {
            points.resize(points.size() + 1);
            points.at(points.size()) = currentTile + 1;
            currentTile++;
        }
    }
    else if (tiles[currentTile - 1] <= 2 && floor(currentTile / height) == floor(currentTile - 1 / height)) {
        for (i = 1; i <= points.size(); i++) {

            if (points[i] == currentTile - 1) {
                possible = false;
            }
            if (possible == true) {
                points.resize(points.size() + 1);
                points.at(points.size()) = currentTile - 1;
                currentTile--;
            }
        }

    }
    else if (tiles[currentTile - height] <= 2) {
        for (i = 1; i <= points.size(); i++) {

            if (points[i] == currentTile - height) {
                possible = false;
            }
            if (possible == true) {
                points.resize(points.size() + 1);
                points.at(points.size()) = currentTile - height;
                currentTile--;
            }
        }

    }
    else if (tiles[currentTile + height] <= 2) {
        for (i = 1; i <= points.size(); i++) {

            if (points[i] == currentTile + height) {
                possible = false;
            }
            if (possible == true) {
                points.resize(points.size() + 1);
                points[points.size()] = currentTile + height;
                currentTile--;
            }
        }

    }

    pointList = points;
}

这给了我一个超出范围的运行时错误向量下标,然后如果我忽略它然后它说立场库超出范围而且这两个都在不在文件中的行上。

在此之前它给了我关于使用

的错误
 points.at[];

所以当我删除它们时,会出现这些错误。

我也有sfml如果重要的话。

提前致谢。

1 个答案:

答案 0 :(得分:0)

在C ++中,有效数组的索引(下标)在[0,array_size-1]范围内。因此,在访问tilespoints元素之前,请确保索引有效。

要在向量的末尾添加新元素,请使用push_back()方法:points.push_back(currentTile - 1);