gpt4 book ai didi

c++ - 从双指针数组读取 - NULL

转载 作者:行者123 更新时间:2023-11-28 06:49:03 25 4
gpt4 key购买 nike

我的方法替换了双数组 _board 中的单元格。替换后,它将单元格设置为 NULL。

在对设置为 NULL 的单元格的下一次调用中,它不会将其识别为 NULL。

((_board[location.getX()][location.getY()]==NULL))

然后继续阅读垃圾的方法

怎么了?

    void Board::MovePiece(Point location, Move m) throw (GameEndException, OutOfBoundsException, InvalidMoveException, NonEmptySquareException)
{
int i,j;
bool flag1,flag2;
if **(_board[location.getX()][location.getY()]==NULL)**{
InvalidMoveException IO;
throw IO;}
if ((_board[location.getX()][location.getY()]->IsValidMove(m)==false)||(_board[location.getX()][location.getY()]->getPlayerNum()!=GetTurn())) {
InvalidMoveException IO;
throw IO;}
if (_board[location.getDestination(m).getX()][location.getDestination(m).getY()]!=NULL) {
if (_board[location.getDestination(m).getX()][location.getDestination(m).getY()]->getPlayerNum()==GetTurn()){
NonEmptySquareException IO;
throw IO;}}
Point tmp=location.getDestination(m);
_board[tmp.getX()][tmp.getY()]=_board[location.getX()][location.getY()]->Clone();
delete _board[location.getX()][location.getY()];
_board[location.getX()][location.getY()]=NULL;
_lastM=m;
_lastP=location;
flag1=false;
flag2=false;
for (i=0;i<BOARD_SIZE;i++){
for (j=0;j<BOARD_SIZE;j++){
if(_board[i][j]!=NULL){
if (_board[i][j]->getPlayerNum()==ONE) flag1=true;
if (_board[i][j]->getPlayerNum()==TWO) flag2=true;
}}}
if ((flag1==false)||(flag2==false)){
GameEndException IO;
throw IO;}
}

最佳答案

这里 delete _board[location.getX()][location.getY()]; 你已经删除了位置并且你正在尝试访问已删除的内存位置。

关于c++ - 从双指针数组读取 - NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24366866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com