gpt4 book ai didi

c++ - 如何将托管数组传递给函数?

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:55 25 4
gpt4 key购买 nike

我正在尝试编写 Tic-Tac-Toe 游戏,但遇到编译器错误。有 3 个与 TTT::whoWins 函数相关的错误。任何人都可以帮助我和/或指出我正确的方向。

错误是:

Error 2 error C2664: 'TTT::checkColumn' : cannot convert parameter 1 from 'cli::array<Type,dimension> ^' to 'int [][3]' C:\Users\Grunt\Desktop\tictactoe game\tictactoe game\TTT.cpp 59
Error 3 error C2664: 'TTT::checkDiagonal' : cannot convert parameter 1 from 'cli::array<Type,dimension> ^' to 'int [][3]' C:\Users\Grunt\Desktop\tictactoe game\tictactoe game\TTT.cpp 59
Error 1 error C2664: 'TTT::checkRow' : cannot convert parameter 1 from 'cli::array<Type,dimension> ^' to 'int [][3]' C:\Users\Grunt\Desktop\tictactoe game\tictactoe game\TTT.cpp 59

代码如下:

TTT::TTT()
{
matrix = gcnew array <int,2>(3,3);
}

bool TTT::isaTie()
{
return 0;
}

bool TTT::isaTie2()
{
return 0;
}

int TTT::whoWins()
{

if((checkRow(matrix,ROW_SIZE))==1 || (checkColumn(matrix,COLUMN_SIZE))==1 || (checkDiagonal(matrix,ROW_SIZE))==1)
return 1;
else
return 0;
}


bool TTT::checkRow (int mat[][COLUMN_SIZE], int ROW_SIZE)
{
int row1 = mat[0][0] + mat[0][1] + mat [0][2];
int row2 = mat[1][0] + mat[1][1] + mat [1][2];
int row3 = mat[2][0] + mat[2][1] + mat [2][2];

if (row1 == 0 || row2 == 0 || row3 == 0)
{
return 1;
}
else if (row1 == 3 || row2 == 3 || row3 == 3)
{
return 1;
}
else
return 0;
}
bool TTT::checkColumn (int mat[][COLUMN_SIZE], int ROW_SIZE)
{
int col1 = mat[0][0] + mat[1][0] + mat [2][0];
int col2 = mat[0][1] + mat[1][1] + mat [2][1];
int col3 = mat[0][2] + mat[1][2] + mat [2][2];

if (col1 == 0 || col2 == 0 || col3 == 0)
{
return 1;
}
else if (col1 == 3 || col2 == 3 || col3 == 3)
{
return 1;
}else
return 0;
}

bool TTT::checkDiagonal (int mat[][COLUMN_SIZE], int ROW_SIZE)
{
int diag1 = mat[0][0] + mat[1][1] + mat [2][2];
int diag2 = mat[2][0] + mat[1][1] + mat [0][2];

if (diag1 == 0 || diag2 == 0)
{
return 1;
}
else if (diag1 == 3 || diag2 == 3)
{
return 1;
}else
return 0;
}

最佳答案

在你的类中,matrixarray 类型,但在你的函数中(例如 checkRow),你的第一个参数是 int 类型。因此,您需要将其更改为 array

另一方面,您的 check* 函数是 TTT 的一部分,因此它们可以直接访问矩阵,不需要它作为参数。

关于c++ - 如何将托管数组传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9771032/

25 4 0
文章推荐: java - 无法卸载Java
文章推荐: html - 我的页眉和
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com