简体   繁体   English

C ++ EXC_BAD_ACCESS(代码= 1访问= 0x0)

[英]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. 我正在为Connect 4游戏编写一个类,当我去画画板时遇到了问题,当我尝试引用game_board成员变量时,错误EXC_BAD_ACCESS(code = 1 access = 0x0)一直出现。 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. 在这里,您要声明一个新的char** game_boardchar** game_board分配,而该类的game_board仍未分配。

I think your intention was to use the class member Board::game_board . 我认为您的意图是使用类成员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]

暂无
暂无

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

相关问题 C ++节点分配错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0) - C++ node assign error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)错误 - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) error C ++:尝试将新节点添加到链接列表会产生“线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)”错误 - C++: Attempting to add new node to linked list yields “Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)” error Cocos2D-X-初始化CCTMXTiledMap时为EXC_BAD_ACCESS(代码= 2,地址= 0x0) - Cocos2D-X - EXC_BAD_ACCESS (code=2, address=0x0) when initializing a CCTMXTiledMap 代码错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0) - code error : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 线程 1:xcode 中的 EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误 - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) error in xcode 线程1:生成EXC_BAD_ACCESS(代码= 1,地址= 0x0)问题 - Thread 1 : EXC_BAD_ACCESS (Code = 1, address = 0x0) issue is generated 共同映射两个向量元素,并得到EXC_BAD_ACCESS(code = 1,address = 0x0)错误 - Comapring two vector elements and get EXC_BAD_ACCESS(code=1, address = 0x0) error Xcode:线程1:制作邻接表时,EXC_BAD_ACCESS(代码= 1,地址= 0x0) - Xcode: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) when making adjacency list 调用 glCreateShader 时出现运行时错误“线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)” - Getting runtime error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)" whencalling glCreateShader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM