【问题标题】:Function checking out of bounds/segmentation fault in 2D loop二维循环中的函数检查越界/分段错误
【发布时间】:2015-12-31 17:12:44
【问题描述】:

我正在循环遍历 Cell 对象的 2D 数组并检查每个对象周围的 Cell。在检查每个单元格之前,我需要确保它存在于数组中。为此,我使用以下功能:

bool Cell::isInBounds(int h, int w) {
    if ((h >= 0 && w >= 0) || (h < HEIGHT && w < WIDTH)) {
        return true;
    }
    return false;
}

它似乎在不应该返回 true 时,我觉得我错过了一些明显的东西。 ih 正在传递,例如,cell[i+1][j+1].isInBounds((i+1), (j+1))

C++ 新手。我在这里做错了什么?

【问题讨论】:

    标签: c++ loops multidimensional-array


    【解决方案1】:

    应该是

     if ((h >= 0 && w >= 0) && (h < HEIGHT && w < WIDTH)) {
    

    【讨论】:

    • Drrp...不敢相信我错过了。谢谢。
    猜你喜欢
    • 2021-05-26
    • 1970-01-01
    • 2011-12-21
    • 2017-09-28
    • 2014-01-03
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2019-11-02
    相关资源
    最近更新 更多