使用数组获取错误C2440,我不知道为什么

时间:2018-12-04 23:47:59

标签: c++ compiler-errors

我正在开发一个非常简单的国际象棋游戏,但我不断出错,我不知道为什么。这是我的代码的一部分:

string board[8][8] = {
"BR",  "BKn",  "BB",  "BQ",  "BKi",  "BB",  "BKn",  "BR", 
"BP",  "BP",  "BP",  "BP",  "BP",  "BP",  "BP",  "BP", 
"0",  "0",  "0",  "0",  "0",  "0",  "0",  "0", 
"0",  "0",  "0",  "0",  "0",  "0",  "0",  "0", 
"0",  "0",  "0",  "0",  "0",  "0",  "0",  "0", 
"0",  "0",  "0",  "0",  "0",  "0",  "0",  "0", 
"WP",  "WP",  "WP",  "WP",  "WP",  "WP",  "WP",  "WP", 
"WR",  "WKn",  "WB",  "WKi",  "WQ",  "WB",  "WKn",  "WR"
};

void setBoard(string piece, string side, int where1, int where2) {
if (side == "white") {
    if (piece == "rook") {
        board[where1, where2] = "WR"; //All lines like this get an error
    }
    else if (piece == "knight") {
        board[where1, where2] = "WKn";
    }
    else if (piece == "bishop") {
        board[where1, where2] = "WB";
    }
    else if (piece == "king") {
        board[where1, where2] = "WKi";
    }
    else if (piece == "queen") {
        board[where1, where2] = "WQ";
    }
    else if (piece == "pawn") {
        board[where1, where2] = "WP";
    }
}
else if (side == "black") {
    if (piece == "rook") {
        board[where1, where2] = "BR";
    }
    else if (piece == "knight") {
        board[where1, where2] = "Bkn";
    }
    else if (piece == "bishop") {
        board[where1, where2] = "BB";
    }
    else if (piece == "king") {
        board[where1, where2] = "BKi";
    }
    else if (piece == "queen") {
        board[where1, where2] = "BQ";
    }
    else if (piece == "pawn") {
        board[where1, where2] = "BP";
    }
}
}

void play(string where) {
bool second = false;
char first = where[0];
int x = conv1(first);

int sec = where[1];
sec -= 48;
int y = conv2(sec);

if (x == 69 || y == 69) {
    cout << "Error: Not a valid space. Make sure the letter is capitalized." << endl;
    chess();
}
else {
    if (board[x][y] != "0") {
        if (second == false) {
            string piece = getPiece(board[x][y]);
            cout << "Where do you want to move the piece " << piece << " ?" << endl;
            string input; cin >> input;
            play(input);
        }
        else if (second == true) {
            string piece = getPiece(board[x][y]);
            cout << "Are you sure you want move the piece " << piece << " to the space " << where << " ?" << endl << "Yes(1) \nNo(2)" << endl;
            int choice; cin >> choice;
            if (choice == 1) {
                string side = getSide(board[x][y]);
                setBoard(piece, side, x, y);
            }
            else chess();
        }
    }
    else cout << "Error: There is no piece on space " << where << " ." << endl;
}
}

这给了我这个错误:

  

错误1错误C2440:'=':无法从'const char [3]'转换为   'std :: string [8]'

我不知道我的代码有什么问题,谁能告诉我发生了什么事。而且,我可以在游戏功能上毫无问题地改变棋盘的价值,只是当我尝试这样做时,它就会生我的气。

1 个答案:

答案 0 :(得分:2)

错误消息是由于使用双索引的方式引起的:

board[where1, where2]

这实际上并没有索引二维数组。相反,,运算符会计算两个操作数,例如where1where2并返回后者的值。

因此表达式的行为与

相同
board[where2]

其类型为std::string[8],因此无法分配类型为const char[3]的字符串文字。

使用两次下标运算符可完成正确的索引编制

board[where1][where2]