Ian Ian - 14 days ago 10
C++ Question

warning: control may reach end of non-void function [-Wreturn-type] }

I am a beginner in c++ and I am trying to make a chess game and when I compile :

bool isValidMove(int inCol, int inRow, int outCol, int outRow, char board[8][8]) {
if(board[inRow][inCol] == '-') {
return false;
}
else {
if((board[inRow][inCol] == 'R' || board[inRow][inCol] == 'r') && isValidMoveRook(inCol, inRow, outCol, outRow, board)) {
return true;
}
if((board[inRow][inCol] == 'K' || board[inRow][inCol] == 'k') && isValidMoveKnight(inCol, inRow, outCol, outRow, board)) {
return true;
}
if((board[inRow][inCol] == 'R' || board[inRow][inCol] == 'r')&& isValidMoveRook(inCol, inRow, outCol, outRow, board)) {
return true;
}
}
}


I get this warning from my compiler
warning: control may reach end of non-void function [-Wreturn-type] }

can anyone help me with this problem by the way I am on the mac.

Answer

You don't return anything at the very end if it doesn't go on your conditions.

bool isValidMove(int inCol, int inRow, int outCol, int outRow, char board[8][8]) {
     if(board[inRow][inCol] == '-') {
         return false;
 }
 else {
     if((board[inRow][inCol] == 'R' || board[inRow][inCol] == 'r') && isValidMoveRook(inCol, inRow, outCol, outRow, board)) {
     return true;
         }
         if((board[inRow][inCol] == 'K' || board[inRow][inCol] == 'k') && isValidMoveKnight(inCol, inRow, outCol, outRow, board)) {
             return true;
         }   
         if((board[inRow][inCol] == 'R' || board[inRow][inCol] == 'r')&&      isValidMoveRook(inCol, inRow, outCol, outRow, board)) {
             return true;
         }
     }
     // return something here
 }