简体   繁体   English

引发了C ++读取访问冲突错误,但是我不确定为什么。 瓷砖滑块益智游戏

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

So I'm coding an object oriented Tile Slider Puzzle game, and I feel as though I've coded things correctly, and when I build the project, no errors are thrown. 因此,我正在编写一个面向对象的Tile Slider Puzzle游戏,感觉好像我已经正确编写了代码,并且在构建项目时,不会引发任何错误。 However, when I go to run my code (Visual Studio 2015 IDE) I get a message box saying the .exe file has stopped working. 但是,当我运行代码(Visual Studio 2015 IDE)时,出现一个消息框,提示.exe文件已停止工作。 Here are my files thus far: 到目前为止,这是我的文件:

The following is the TileSlider.h file: 以下是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

The following is the TileSlider.cpp file: 以下是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;
}

The following is the driver file for my code (TileSliderGame.cpp): 以下是我的代码(TileSliderGame.cpp)的驱动程序文件:

#include "TileSlider.h"
using namespace std;

int main() {
    TileSlider* game = new TileSlider();
    game->playGame();
    delete game;
    return 0;
}

To attempt to determine the issue that was occurring, I put a break point to step into where I'm calling playGame() in the driver file (TileSliderGame.cpp). 为了尝试确定发生的问题,我在驱动程序文件(TileSliderGame.cpp)中的调用playGame()处放置了一个断点。 I stepped into that function, and then stepped into where playGame() calls the printBoard(consoleOut) function and I received a Read Access Violation error when I got to this line: 我进入该函数,然后进入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
. . .

(The error was thrown at the last line shown above) Error message: (错误显示在上面显示的最后一行)错误消息:

Exception thrown: read access violation. 引发异常:读取访问冲突。

this->gameBoard was 0x1110112. this-> gameBoard为0x1110112。

Now, I'm really not sure why I would get a read access violation error within the printBoard() function because it's a private function, and therefore should be able to directly access the private gameBoard variable inside the class. 现在,我真的不确定为什么在printBoard()函数中会出现读取访问冲突错误,因为它是私有函数,因此应该能够直接访问类内的私有gameBoard变量。 I even tried to see if it would make a difference to create an accessor for the gameBoard, but it didn't (the same error was thrown). 我什至试图查看为gameBoard创建访问器是否会有所不同,但是没有(抛出相同的错误)。

Another note I'd like to make, I started this code in a separate project with an Imperative program design and have got it running as I intend it to. 我想做的另一个说明是,我在一个具有Imperative程序设计的项目中启动了此代码,并使其按预期运行。 Therefore, I know that the code within my object oriented program regarding how the TileSlider game works is working perfectly fine. 因此,我知道我的面向对象程序中有关TileSlider游戏如何工作的代码可以很好地工作。 I'm just not sure what I may have done wrong when I redesigned the code into an object oriented design. 我不确定将代码重新设计为面向对象设计时可能做错了什么。

If what my game is supposed to look like is confusing, the TileSlider gameBoard is a 3x3 2D character array that displays onto the screen like so : 如果我的游戏看起来令人困惑,则TileSlider gameBoard是一个3x3 2D字符数组,其显示在屏幕上的方式如下:

1 2 3
4 5 6
7 8 *

Above is how the gameBoard starts, it is then scrambled and the user then moves the tiles with the "wasd" keys to attempt to win the game. 以上是gameBoard的启动方式,然后进行了加扰,然后用户使用“ wasd”键移动了图块以尝试赢得游戏。 Any tiles moved into their correct position (the positions showed above) are colored green, and any tiles that aren't in their correct position are colored red. 移动到正确位置(上面显示的位置)的所有图块都被涂成绿色,而未处于正确位置的图块被涂成红色。 The one exception is that the empty tile (the asterisk) is printed in white all of the time. 一个例外是,空块(星号)始终以白色打印。

I don't think that my code is perfect so I'll take any constructive criticism on my code and code design that I can get. 我认为我的代码并不完美,因此我会对我的代码和代码设计提出任何建设性的批评。

Edit: I removed a large portion of my TileSlider.cpp file code shown above because it was irrelevant to the error I made in my code. 编辑:我删除了上面显示的TileSlider.cpp文件代码的很大一部分,因为它与我在代码中所做的错误无关。

You wrote: 你写了:

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

You probably meant: 您可能的意思是:

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

The reason is that your declaration of solvedBoard and gameBoard in TileSlider::setUpBoards() effectively hides the TileSlider member variables with the same names, and nothing is assigned to the latter. 原因是您在TileSlider::setUpBoards()声明solvedBoardgameBoard有效地隐藏具有相同名称的TileSlider成员变量,而没有为后者分配任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM