非空函数错误

Non-void function error

我正在尝试用 c 语言编译这个程序(对于在线 cs50 class),并不断收到以下错误:

fifteen.c:233:1: 错误:控制可能到达非空函数的结尾 [-Werror,-Wreturn-type] }

我不是在询问我的代码的逻辑,而是在寻找我在过去一小时内遗漏的语法错误。代码如下。谢谢!

bool move(int tile)
{
    for (int i = 0; i < d - 1; i++){

        for (int j= 0; j < d - 1; j++){

            if (tile == board[i][j]){

                if (board[i+1][j] == tile){
                    board[i+1][j] = 0;
                    board[i+1][j] = tile;
                    return true;
                                        }
                else if (board [i-1][j] == 0){
                    board[i-1][j] = tile;
                    board [i-1][j] = 0;
                    return true;
                                        }
               else if (board [i][j+1] == 0){
                    board[i][j+1] = tile;
                    board [i][j+1] = 0;
                    return true;
                                        }
               else if (board [i][j-1] == 0){
                    board[i][j-1] = tile;
                    board [i][j-1] = 0;
                    return true;
                                      }
    }
    return false;
}
}
}

此错误表明您的方法可以在不返回 值的情况下到达终点。在这种情况下会发生这种情况,因为您的括号不合适。

尝试以下操作:

bool move(int tile) {
     for (int i = 0; i < d - 1; i++) {
          for (int j = 0; j < d - 1; j++) {
              if (tile == board[i][j]) {
                  if (board[i+1][j] == tile) {
                      board[i+1][j] = 0;
                      board[i+1][j] = tile;
                      return true;
                  }
                  else if (board [i-1][j] == 0){
                      board[i-1][j] = tile;
                      board [i-1][j] = 0;
                      return true;
                  }
                  else if (board [i][j+1] == 0){
                      board[i][j+1] = tile;
                      board [i][j+1] = 0;
                      return true;
                  }
             }
             else if (board [i][j-1] == 0){
                 board[i][j-1] = tile;
                 board [i][j-1] = 0;
                 return true;
             }
         }
    }
    return false;
}