Ian Ian - 1 month ago 19
C++ Question

error: function definition is not allowed here(what is wrong) c++

I am a beginner in c++ and I am trying to make a chess game I is not finished yet. When I try to compile the code below it gives me
error: function definition is not allowed here. for isValidMove, movePiece, and Main
Can anyone tell me why it gives me that.

The code is :

#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctype.h>
#include <cmath>
#include <algorithm>

using namespace std;

char board[8][8] = {
'R','N','B','Q','K','B','N','R',
'P','P','P','P','P','P','P','P',
'-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r'};

void swap(char board[8][8], int inRow, int inCol, int outRow, int outCol) {
char tmp = board[inRow][inCol];
board[inRow][inCol] = board[outRow][outCol];
board[outRow][outCol] = tmp;
}

void printBoard(char board[8][8]) {
cout << "# a b c d e f g h " << endl;
for(int i = 0; i <= 7; i++) {
cout << 8 - i << " ";
for(int j = 0; j <= 7; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}

bool isValidMoveRook(int inCol, int inRow, int outCol, int outRow, char board[8][8]) {
int smallCol = min(inCol, outCol);
int smallRow = min(inRow, outRow);
int maxCol = max(inCol, outCol);
int maxRow = max(inRow, outRow);
if(inRow == outRow) {
for(int i = smallCol + 1; i < maxCol; i++) {
if(board[inRow][i] != '-') {
return false;
}
}
return true;
}
else if(outCol == inCol) {
for(int i = smallRow + 1; i < maxRow; i++) {
if(board[i][inCol] != '-') {
return false;
}
}
return true;
}
else {
return false;
}
}
bool isValidMoveBishop(int inCol, int inRow, int outCol, int outRow, char board[8][8]) {
int rowDiff = outRow - inRow;
int colDiff = outCol - inCol;
if(abs(outRow - inRow) == abs(outCol - inCol)) {
if(rowDiff < 0 && colDiff < 0) {
for(int i = 1; i > rowDiff; i--) {
if(board[inRow + rowDiff][inCol + rowDiff] != '-') {
return false;
}
}
}
}

else {
return true;
}
}

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


void movePiece(char board[8][8]) {
string input;
string output;
cout << "Please enter your piece position : ";
cin >> input;
cout << "Please enter where you want to place your piece : ";
cin >> output;

int inCol = input[0] - 'a';
int inRow = 7 - (input[1] - '1');
int outCol = output[0] - 'a';
int outRow = 7 - (output[1] - '1');
while(!isValidMove(inCol, inRow, outCol, outRow, board)) {
cout << "Invalid input try again" << endl;
cout << "Please enter your piece position : ";
cin >> input;
cout << "Please enter where you want to place your piece : ";
cin >> output;
inCol = input[0] - 'a';
inRow = 7 - (input[1] - '1');
outCol = output[0] - 'a';
outRow = 7 - (output[1] - '1');
}
if(board[outRow][outCol] == '-') {
swap(board, inRow, inCol, outRow, outCol);
}
else {
if(isupper(board[inRow][inCol]) != isupper(board[outRow][outCol])) {
board[outRow][outCol] = board[inRow][inCol];
board[inRow][inCol] = '-';
}
}
}

int main() {
printBoard(board);
while (true) {
movePiece(board);
printBoard(board);
}
}

Answer

You are missing a closing } in isValidMoveBishop. Thus, the function's definition is not ended, and the compiler thinks you're trying to define another function inside it:

bool isValidMoveBishop(int inCol, int inRow, int outCol, int outRow, char board[8][8]) {
    int rowDiff = outRow - inRow;
    int colDiff = outCol - inCol;
    if(abs(outRow - inRow) == abs(outCol - inCol)) {
        if(rowDiff < 0 && colDiff < 0) {
            for(int i = 1; i > rowDiff; i--) {
                if(board[inRow + rowDiff][inCol + rowDiff] != '-') {
                    return false;
                }
            }
        }
    } // This closing } was missing in the OP
    else {
        return true;
    }
}