国际象棋游戏-程序崩溃 我正在用C++做一个象棋游戏。 我有以下课程: class piece { protected : point position ; char represent ; bool colour; // true for player 1 = green , false for player 0 = blue public : virtual ~piece() ; bool find_player(piece* to_check, int num); char get_char() ; bool get_colour() ; virtual bool move_setup(point source, point dest, piece* arr[][SIZE] ) = 0 ; virtual bool move(point source, point dest, piece* arr[][SIZE]) = 0; bool same_player(point dest, piece* arr[][SIZE], int* num); }; class board { private: piece* arr[SIZE][SIZE] ; public : board(); void free_borad(); void set_piece(int x , int y , int type , bool colour ); void print_board(); void move(point source , point dest); void set_colour(int i , int j , bool reset ); }; class pawn : public piece { public : pawn(point position , bool colour ); ~pawn(); virtual bool move_setup(point source, point dest, piece* arr[][SIZE]); virtual bool move(point source, point dest, piece* arr[][SIZE]); bool if_forward(point source, point dest); bool if_diagonal(point source, point dest, int* offset); };

国际象棋游戏-程序崩溃 我正在用C++做一个象棋游戏。 我有以下课程: class piece { protected : point position ; char represent ; bool colour; // true for player 1 = green , false for player 0 = blue public : virtual ~piece() ; bool find_player(piece* to_check, int num); char get_char() ; bool get_colour() ; virtual bool move_setup(point source, point dest, piece* arr[][SIZE] ) = 0 ; virtual bool move(point source, point dest, piece* arr[][SIZE]) = 0; bool same_player(point dest, piece* arr[][SIZE], int* num); }; class board { private: piece* arr[SIZE][SIZE] ; public : board(); void free_borad(); void set_piece(int x , int y , int type , bool colour ); void print_board(); void move(point source , point dest); void set_colour(int i , int j , bool reset ); }; class pawn : public piece { public : pawn(point position , bool colour ); ~pawn(); virtual bool move_setup(point source, point dest, piece* arr[][SIZE]); virtual bool move(point source, point dest, piece* arr[][SIZE]); bool if_forward(point source, point dest); bool if_diagonal(point source, point dest, int* offset); };,c++,crash,runtime-error,chess,C++,Crash,Runtime Error,Chess,我有一个类,用于实现move函数的每个片段。 这些类是不相关的,所以我只是以pawn类为例。 板移动功能: void board::move(point source, point dest) { if (arr[source.get_x()][source.get_y()]) // if not empty { int x = source.get_x(), y = source.get_y() ; if (arr[x][y]->move_set

我有一个类,用于实现move函数的每个片段。 这些类是不相关的,所以我只是以pawn类为例。 板移动功能:

void board::move(point source, point dest)
{
   if (arr[source.get_x()][source.get_y()]) // if not empty
   {
       int x = source.get_x(), y =  source.get_y() ;
       if (arr[x][y]->move_setup(source, dest, arr ) )  // if the piece  can move there 
       {
           delete arr[dest.get_x()][dest.get_y()];
           arr[dest.get_x()][dest.get_y()] = arr[source.get_x()][source.get_y()];
           arr[source.get_x()][source.get_y()] = NULL;
           std::cout << " Succes! " << std::endl;
       }
       else
       {
        // error
       }
   }
   else // empty
   {
        // error
   }
我已使用VS调试器对其进行了调试,并意识到在发送数组时会发生崩溃,错误消息:

访问冲突读取位置0xFFFFFF

我试着用很多不同的方式发送,但没有任何效果, 但这种方法正是我的一个朋友做的,而且对他来说效果很好。 有人能帮忙吗?
谢谢。

C++中的数组是0的,这意味着第一个元素的索引是0。如果您的坐标是基于1的,则表示您正在访问数组中一行一列之外的元素。这反过来又会导致突破阵列的边界并导致崩溃。解决方案是在访问阵列时采用基于0的坐标系或从x/y坐标中减去1。

在构建电路板时,是否将“arr”中的所有值初始化为NULL/nullptr?是的,所有不包含典当对象的正方形(我仅使用一块进行调试)都有一个NULL,我打印电路板,它可以工作,所以我确信这一点。另一种可能是一个经典的错误。您的x/y坐标是基于0还是基于1?也就是说,棋子的坐标是从棋盘上的点(0,0)开始还是从(1,1)开始?哇,我不敢相信,你说得对!谢谢:)我确定arr[x][y]不是空的,x和y是我要检查的值,为什么它不工作?非常感谢你的帮助!
if (arr[x][y]->move_setup(source, dest, arr ) )