source.cpp:LINE:COL:错误:此处不允许使用函数定义

时间:2016-10-29 14:54:54

标签: c++ c

我是c ++的初学者,我正在尝试制作国际象棋游戏,但我尚未完成。当我尝试编译下面的代码时,它给了我 错误:此处不允许使用函数定义。 for isValidMove,movePiece和Main 任何人都可以告诉我为什么它会给我这个。

代码是:

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

1 个答案:

答案 0 :(得分:1)

您错过}中的结束isValidMoveBishop。因此,函数的定义没有结束,并且编译器认为您正在尝试定义另一个内部函数:

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