c++ - 为什么我用 "randomSelect"方法替换几行代码后程序无法运行

标签 c++ methods grid

The scatter method takes the original image and scatter its pixels. The program works well when I use several lines of code instead of the method "randomSelect". The program seems to go into an infinite loop and the image does not change when I use the method "randomSelect".

void scatter(GBufferedImage &img, Grid<int> original, int row, int col) {
    int degree;
    while (true) {
        degree = getInteger("Enter degree of scatter [1-100]: ");
        if (degree >=1 && degree <= 100) break;
    }

    Grid<int> newImg(row, col);


    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {

      /*    int newRow = -1;
            int newCol = -1;
            while (!original.inBounds(newRow, newCol)) {
                newRow = randomInteger(max(i - degree, 0), min(i + degree,original.numRows()));
                newCol = randomInteger(max(j - degree, 0), min(j + degree,original.numRows()));
            }
            newImg[i][j] = original[newRow][newCol];   */    // work properly

        newImg[i][j] = randomSelect(original, i , j, degree);  // do not work

        }
    }

    img.fromGrid(newImg);
}



int randomSelect(Grid<int> original, int i, int j, int degree) {     // do not work
    int newRow = -1;
    int newCol = -1;
    while (!original.inBounds(newRow, newCol)) {
        newRow = randomInteger(max(i - degree, 0), min(i + degree,original.numRows()));
        newCol = randomInteger(max(j - degree, 0), min(j + degree,original.numRows()));
    }

    return original[newRow][newCol];
}

最佳答案

您应该传递原件作为引用:

int randomSelect(Grid<int>& original, int i, int j, int degree) {     // will work

关于c++ - 为什么我用 "randomSelect"方法替换几行代码后程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304500/

相关文章:

c++ - g++ 4.6 没有 <bits/c++config.h> 头文件 cstring 要求的文件

python - 创建类后在方法上设置属性会引发 "' instancemethod' object has no attribute“但属性显然存在

python - 使用实例属性中的关键字的方法

javascript - 标题中大写字母之间的 Angular UI 网格空间

python - 从内容中查找字典

c++ - 执行双浮点除法的正确算法是什么?

c++ - Visual C++ 2008 问题

c# - Visual Studio Intellisense 不显示泛型重载的方法

ios - 在 iOS 上实现按钮网格

c++ - 如何将 int 从一个 .h 转换为另一个 .h