C++ 比较if语句中的枚举类型时出错
每当我尝试使用findPieceAt函数在数组中给我一个位置时,我都会得到一个错误。在下面的代码中C++ 比较if语句中的枚举类型时出错,c++,C++,每当我尝试使用findPieceAt函数在数组中给我一个位置时,我都会得到一个错误。在下面的代码中 if (findPieceAt(newRow, newCol) != -1) { if (chessPieces[findPieceAt(newRow, newCol)].getColor == turn) //line with error { cout << "invalid attack on same color"
if (findPieceAt(newRow, newCol) != -1)
{
if (chessPieces[findPieceAt(newRow, newCol)].getColor == turn) //line with error
{
cout << "invalid attack on same color" << endl;
return false;
}
else
{
if (chessPieces[findPieceAt(oldRow, oldCol)].captureMove(newRow, newCol) == true);
{
chessPieces[findPieceAt(newRow, newCol)].isCaptured;
cout << "capture of " << chessPieces[findPieceAt(newRow, newCol)].getFullName << " is successful" << endl;
return true;
}
}
}
转弯定义如下
void Chess::gameLoop(istream & input)
{
Color turn = Black; // Black will start the game
棋子阵列
private:
static const int NUM_ROWS = 8, NUM_COLS = 8;
static const int NUM_CHESS_PIECES = 32;
ChessPiece chessPieces[NUM_CHESS_PIECES];
};
已使用的枚举类型
enum ChessPieceType { Pawn, Rook, Knight, Bishop, Queen, King };
enum Color { White, Black };
缺少括号,因为
getColor
是一个函数调用:
if (chessPieces[findPieceAt(newRow, newCol)].getColor() == turn )
另外,您可以缓存findPieceAt(newRow,newCol)
的值,而不是在代码中重复调用它
此外,由于您没有发布包含错误的整个函数,因此需要确保在所有退出点返回
bool
。如果findPieceAt(newRow,newCol)
返回-1,那么您发布的代码可能不会返回任何内容。不从具有返回类型的函数返回值是未定义的行为。您会得到什么错误?“错误C3867:“ChessPiece::getColor”:函数调用缺少参数列表;使用“&ChessPiece::getColor”创建指向成员的指针“我还收到了另一个错误”“错误C2678:二进制“=”:未找到接受“重载函数”类型的左侧操作数的运算符(或没有可接受的转换)”,如果再次读取该错误消息,您认为可能是什么?特别是当您查看错误所在的行时。
enum ChessPieceType { Pawn, Rook, Knight, Bishop, Queen, King };
enum Color { White, Black };
if (chessPieces[findPieceAt(newRow, newCol)].getColor() == turn )