-1

我已经使用 Visual Studio 2017(社区版)几个月了,做了几个应用程序,但从来没有接近过我在这里遇到的这个编译错误。我发现了一些与我的错误类似的线程,但它们都没有真正帮助。

今天我开始开设视觉工作室,并决定制作一款经典的井字游戏。我开始对其进行编码,但我从未真正时不时地进行调试。当我最终编译并测试程序时,我得到了这个错误:

Exception thrown at 0x00007FFC120B0FE1 (ucrtbased.dll) in 
CppTicTacToe.exe: 0xC0000005: Access violation reading location 
0x0000000000000000.

我想这是由于语法违规造成的,但不知道它的实际含义。从控制台的一侧,它开始然后停止,但实际上并没有关闭。这是代码,它似乎工作正常:

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

using namespace std;
using byte = unsigned char;

const enum letter { x = 'x', o = 'o' };

static letter human(x);
static letter bot(o);

const unsigned short _x = 3;
const unsigned short _y = 3;

const byte* board[_x][_y] =
{
    { 0, 0, 0 },
    { 0, 0, 0 },
    { 0, 0, 0 }
};

const unsigned char& get_board_value(unsigned short &x, unsigned short &y) {
    return *board[x][y];
}

void put_board_char(const byte* (&_char), unsigned short &x, unsigned short &y) {
    board[x][y] = _char;
}

void put_board_chars(const byte* (&chars)[_x][_y], unsigned short &chars_cnt_x, unsigned short &chars_cnt_y) {
    unsigned short x1, y1 = 0;

    for (x1 = 0; x1 < chars_cnt_x; x1++) {
        for (y1 = 0; y1 < chars_cnt_y; y1++) {
            put_board_char(chars[x1][y1], x1, y1);
        }
    }
}

void draw_board() {
    unsigned short x1, y1 = 0;

    for (x1 = 0; x1 < _x; x1 += 1) {
        cout << "-----" << endl;
        for (y1 = 0; y1 < _y; y1 += 1) {
            if (y1 == 3 || y1 == 6) {
                cout << board[x1][y1] << endl;
                continue;
            }
            cout << board[y1][x1] << "|";
        }
    }
}

void ai(void){
    //todo
}

void logic(void) {
    //todo
}

int main(int argc, const char ** argv[]) {

    while (1) {
        draw_board();
    }

    return 0;
}
4

1 回答 1

1
const byte* board[_x][_y] =
{
    { 0, 0, 0 },
    { 0, 0, 0 },
    { 0, 0, 0 }
};

您将 的元素初始化board为 0,因此nullptr.

draw_board()

cout << board[y1][x1] << "|";

board[y1][x1]nullptr,然后您尝试从中读取。

于 2017-04-14T06:50:47.967 回答