简体   繁体   English

C ++-访问冲突阅读

[英]C++ - Access violation reading

I am getting a "Access violation reading location 0x007CE4F8" (print of the error at the end of the post) right after running and choosing a place to put my marker. 运行并选择放置标记的位置后,我立即得到“访问冲突读取位置0x007CE4F8” (错误的打印在帖子末尾)

I am a begginer c++ programmer so would love if you could keep the explanation the simplest possible. 我是一名初学者C ++程序员,所以如果您能使解释尽可能简单,将不胜感激。

main.cpp main.cpp

    #include <iostream>
    #include "playGame.h"

    using namespace std;

    int main()
    {

        playGame game;
        game.play();

        system("PAUSE");
        return 0;
    }

playGame.cpp playGame.cpp

#include "playGame.h"
#include <iostream>

using namespace std;

playGame::playGame()
{
}

bool playGame::play()
{
    char player1 = 'X';
    char player2 = 'O';
    bool gameOver = false;
    int turn = 0;

    char currentPlayer = player1;

    clearBoard();

    while (gameOver == false)
    {
        printBoard();

        x = getX();
        y = getY();

        while (placeMarker(x, y, currentPlayer) == false)
        {
            cout << "***Place already taken!***" << endl;
            x = getX();
            y = getY();
        }
            turn++;

    }
    return gameOver;
}

void playGame::clearBoard()
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = ' ';
        }
    }
}

void playGame::printBoard()
{
    cout << endl;
    cout << " |1 2 3|\n";
    for (int i = 0; i < 3; i++) {
        cout << " -------\n";
        cout << i + 1 << "|" << board[i][0] << "|" << board[i][1] << "|" << board[i][2] << "|\n";
    }
    cout << " -------\n";
}

int playGame::getX()
{
    while ((x < 1) || (x > 3)) {
        cout << "Choose X coordinate (1 - 3): ";
        cin >> x;
        if ((x < 1) || (x > 3))
        {
            cout << "Bad input" << endl;
        }
        else
        {
            x--;
            return x;
        }
    }

}

int playGame::getY()
{
    while ((y < 1) || (y > 3)) {
        cout << "Choose Y coordinate (1 - 3): ";
        cin >> y;
        if ((y < 1) || (y > 3))
        {
            cout << "Bad input" << endl;
        }
        else
        {
            y--;
            return y;
        }
    }
}

bool playGame::placeMarker(int x, int y, char currentPlayer)
{
    if (board[y][x] != ' ')
    {
        return false;
    }
        board[y][x] = currentPlayer;
        return true;
}

playGame.h playGame.h

#pragma once
#include <iostream>

using namespace std;

class playGame
{
public:
    playGame();
    bool play();

private:
    void clearBoard();
    void printBoard();
    int getX();
    int getY();
    bool placeMarker(int x, int y, char currentPlayer);

    char board[3][3];
    int x, y;

};

Posting a print if makes your lives better: Image of the error 发布印刷品以使您的生活更美好: 错误图片

I see couple of problems: 我看到几个问题:

  1. You haven't initialized the member variables of playGame in the constructor. 您尚未在构造函数中初始化playGame的成员变量。 Using values of uninitialized member variables is cause for undefined behavior. 使用未初始化的成员变量的值会导致未定义的行为。

  2. You don't have a return statement in all branches of getX() and getY() . getX()getY()所有分支中都没有return语句。 That is cause for undefined behavior. 这是导致未定义行为的原因。

Update the constructor to have sensible initial values. 更新构造函数以使其具有合理的初始值。

playGame::playGame() : x(0), y(0)
{
   for ( int i = 0; i < 3; ++i )
   {
      for ( int j = 0; j < 3; ++j )
      {
         board[i][j] = ' ';
      }
   }
}

Update getX() to: getX()更新为:

int playGame::getX()
{
   // Make x invalid before starting the while loop.
   // Otherwise, the last valid value will be returned.
   x = 0;
   while ((x < 1) || (x > 3))
   {
      cout << "Choose X coordinate (1 - 3): ";
      cin >> x;
      if ((x < 1) || (x > 3))
      {
         cout << "Bad input" << endl;
      }
   }

   x--;
   return x;
}

Update getY() similarly. 类似地更新getY()

在构造函数中将x和y初始化为例如0(超出1-3的范围),并在放置标记后将它们重置为0。

I don't know if this can explain your problem but in your code I see a thing that I found a little weird. 我不知道这是否可以解释您的问题,但是在您的代码中我看到了一件我觉得很奇怪的东西。

You define x and y as members of the playGame class. 您将xy定义为playGame类的成员。

So, when in play() you write 因此,在play()您可以编写

    x = getX();
    y = getY();

and in getX() and getY() you write 并在getX()getY()编写

    return x;

and

    return y;

you're copying member x in member x through getX() and member y in member y through getY() . 您正在通过getX()复制成员x中的成员x ,并通过getY()复制成员y中的成员y

Suggestion: delete x and y members from playGame class and define x and y as local variables in methods. 建议:从playGame类中删除xy成员,并将xy定义为方法中的局部变量。

Protect your function placeMarker to be sure that your are not out of bounds when you access to your array, ie: 保护您的功能placeMarker,以确保您在访问数组时不会超出范围,即:

if(x > MAX_X || x< MIN_X) return false;
if(y > MAX_Y || y< MIN_Y) return false;

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

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