简体   繁体   中英

C++ EXC_BAD_ACCESS (code=1 access=0x0)

I'm writing a class for a connect 4 game, I am running into problems when I go to draw the board, the error, EXC_BAD_ACCESS (code=1 access=0x0), keeps coming up when I try to reference my game_board member variable. What am I doing wrong? Thanks in advance.

#include "board.h"
#include <iostream>
using namespace std;

Board::Board()
{
char** game_board=new char*[SIZEX];
for(int i=0; i<SIZEX; i++)
    game_board[i]=new char[SIZEY];


for (int i=0;i<SIZEX;i++)
 {
    for (int j=0;j<SIZEY;j++)
    {
        game_board[i][j]=' ';
    }
 }
};
void Board::draw()
{
    for (int j=0; j<SIZEY;++j )
    {
       cout<<"|---+---+---+---+---+---+---|\n";
        for (int i=0; i<SIZEX;++i)
        {
            cout<<"| "<<game_board[i][j]<<" ";
        }
        cout<<"|\n";
   }
    cout<<"|---+---+---+---+---+---+---|\n";
};
Board::~Board()
{
    for(int i=0; i<SIZEX; i++)
        delete [] game_board[i];

    delete []game_board;

};
Board::Board()
{
    char** game_board=new char*[SIZEX];
    ^^^^^^

Here you are declaring a new char** game_board and allocating for it while your class's game_board is still unallocated.

I think your intention was to use the class member Board::game_board . In that case you should not declare it but just use it as it is ( it is already declared ).

Board::Board()
{
   game_board=new char*[SIZEX];  // Or this->game_board=new char*[SIZEX]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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