无效功能错误

时间:2017-02-08 04:24:45

标签: cs50

我试图在c中编译这个程序(对于cs50在线课程),并且不断收到以下错误:

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;
}
}
}

1 个答案:

答案 0 :(得分:0)

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

尝试以下方法:

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;
}