C++ 抛出读取访问冲突错误,但我不确定为什么。平铺滑块益智游戏
C++ Read Access Violation error was thrown, but I'm not sure why. Tile Slider Puzzle Game
所以我正在编写一个面向对象的瓷砖滑块益智游戏,我觉得我编码正确,当我构建项目时,没有抛出任何错误。但是,当我去运行我的代码(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()
中声明solvedBoard
和gameBoard
有效地隐藏了具有相同名称的TileSlider
成员变量,并且没有为后者分配任何内容。
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- 贪吃蛇游戏 - 为什么蛇会跳过瓷砖?
- 为什么我的石头剪刀布游戏不起作用?
- 为什么这个经过的时间(帧时间)计算会锁定我的游戏
- 为什么我的 UWP 游戏在发布时比在调试模式下慢
- 自动点击器在游戏中 - 为什么不起作用?
- 为什么我的 nim 游戏总是选择桩 A?
- 为什么要对固定时间步的游戏循环使用集成?(游戏上的头目)
- C++ 抛出读取访问冲突错误,但我不确定为什么。平铺滑块益智游戏
- 我正在制作一个刽子手游戏,我需要帮助弄清楚为什么它不能完全工作
- 为什么 c++ 游戏不能使用计时器?
- 在此过程中的纸牌游戏程序.不知道为什么我会得到一个随机数-858993460
- 为什么游戏开发者使用C++,为什么其他行业不使用?
- 为什么我的游戏制造商DLL会违反访问权限
- 为什么c++游戏与unity3d相比如此简单
- 为什么我的TicTacToe游戏在生成随机移动时会卡住
- 为什么我的游戏循环不执行
- 为什么我的代码只检查我的刽子手游戏(C ++)中单词的第一个字母
- 为什么我的 SFML 游戏只能在终端中运行时使用
- c++:1hh0益智游戏