C++ 抛出读取访问冲突错误,但我不确定为什么。平铺滑块益智游戏

C++ Read Access Violation error was thrown, but I'm not sure why. Tile Slider Puzzle Game

本文关键字:为什么 益智游戏 不确定 读取 访问冲突 错误 C++      更新时间:2023-10-16

所以我正在编写一个面向对象的瓷砖滑块益智游戏,我觉得我编码正确,当我构建项目时,没有抛出任何错误。但是,当我去运行我的代码(Visual Studio 2015 IDE)时,我收到一个消息框,指出.exe文件已停止工作。以下是我到目前为止的文件:

以下是 TileSlider.h 文件:

#ifndef TILESLIDER_H
#define TILESLIDER_H
#include <Windows.h>
class TileSlider
{
private:
char** solvedBoard;
char** gameBoard;
//mutator(s)
void setUpBoards(); //keep or copy code to constructor
//other member functions
void printBoard(const HANDLE &consoleOut) const; 
void scrambleBoard();
bool isBoardSolved() const;
void makeMove(int move);
public:
TileSlider();   //allocate mem here? maybe call setUpBoards()
~TileSlider();   //deallocate mem here
void playGame();
};
#endif

以下是 TileSlider.cpp 文件:

#include "TileSlider.h"
using namespace std;
#define SIZE 3      //num of rows and cols to board
// --------------------------------------
//            Private Members
// --------------------------------------
// Mutator(s)
void TileSlider::setUpBoards() {
//allocate memory for boards
char** solvedBoard = new char*[SIZE];   
char** gameBoard = new char*[SIZE];
for (int i = 0; i < SIZE; i++) {
solvedBoard[i] = new char[SIZE];
gameBoard[i] = new char[SIZE];
}
//fill the boards
char i = 49;  // ASCII code for '1'
for (int row = 0; row < SIZE; row++) {
for (int column = 0; column < SIZE; column++) {
gameBoard[row][column] = i;
solvedBoard[row][column] = i;
i++;
}
}
gameBoard[SIZE - 1][SIZE - 1] = 42; // ASCII for '*'
solvedBoard[SIZE - 1][SIZE - 1] = 42;
}

以下是我的代码(TileSliderGame.cpp)的驱动程序文件:

#include "TileSlider.h"
using namespace std;
int main() {
TileSlider* game = new TileSlider();
game->playGame();
delete game;
return 0;
}

为了尝试确定发生的问题,我放置了一个断点,以步入驱动程序文件 (TileSliderGame.cpp) 中调用 playGame() 的位置。我步入该函数,然后步入playGame()调用printBoard(consoleOut)函数的位置,当我到达此行时,我收到读取访问冲突错误:

// Other Private Member Functions
void TileSlider::printBoard(const HANDLE &consoleOut) const {
for (int row = 0; row < SIZE; row++) {
for (int column = 0; column < SIZE; column++) {
if (gameBoard[row][column] == 42) { //ASCII code 42 is '*' asterisk
. . .

(错误是在上面显示的最后一行抛出的) 错误信息:

引发异常:读取访问冲突。

这个>游戏板是0x1110112。

现在,我真的不确定为什么我会在 printBoard() 函数中收到读取访问冲突错误,因为它是一个私有函数,因此应该能够直接访问类中的私有游戏板变量。我什至试图看看为 gameBoard 创建一个访问器是否会有所作为,但它没有(抛出了同样的错误)。

我想做的另一个说明是,我在一个单独的项目中启动了这段代码,并按照我的意图运行了它。因此,我知道我的面向对象程序中关于 TileSlider 游戏如何工作的代码运行良好。我只是不确定当我将代码重新设计为面向对象设计时我可能做错了什么。

如果我的游戏应该是什么样子令人困惑,那么 TileSlider 游戏板是一个 3x3 2D 字符数组,它显示在屏幕上,如下所示:

1 2 3
4 5 6
7 8 *

以上是游戏板的启动方式,然后被打乱,然后用户使用"wasd"键移动瓷砖以尝试赢得游戏。移动到正确位置(如上所示位置)的任何磁贴都显示为绿色,任何未位于正确位置的磁贴都显示为红色。唯一的例外是空磁贴(星号)始终以白色打印。

我不认为我的代码是完美的,所以我会对我的代码和代码设计提出任何建设性的批评。

编辑:我删除了上面显示的TileSlider.cpp文件代码的大部分,因为它与我在代码中犯的错误无关。

你写道:

char** solvedBoard = new char*[SIZE];   
char** gameBoard = new char*[SIZE];

你可能的意思是:

solvedBoard = new char*[SIZE];   
gameBoard = new char*[SIZE];

原因是您在TileSlider::setUpBoards()中声明solvedBoardgameBoard有效地隐藏了具有相同名称的TileSlider成员变量,并且没有为后者分配任何内容。