Printing a 2D Dynamic Array Class

Im coding a game called Gomoku(Gobang) and i have used a 2d dynamic array to implement the board in a class, however my code doesnt print the board and crashes when i cout the board in a function. Help! The error occurs in the printBoard() function at this line *cout<<board[i][j];* The code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  int main(void)
{
    int sizeBoard;
    ifstream infile ("input.txt");
    infile >> sizeBoard;
    int rows=15;
    int columns=15;
    Gobang game(sizeBoard,rows,columns);
    game.initialization();
    game.boardReset();
    game.printBoard();
}
//Class Implementation code
#include "Gobang.h"
#include <iostream>
#include <cstdlib>
using namespace std;

Gobang::Gobang()
{
    board = NULL;
}

Gobang:: Gobang(int boardSize,int rows,int columns )
{
    setParameters(boardSize,rows,columns);
}

void Gobang:: setParameters(int boardSize,int rows,int columns)
{
    row = rows;
    sizeB = boardSize;
    cols = columns;
}

Gobang::~Gobang()
{
    board = NULL;
}

void Gobang:: initialization()
{
    char  **board = new char*[row];
    //Creation of Board in Dynamic memory
    for (int i=0; i< sizeB ; i++ )
    {
        board[i] = new char[cols];
    }

    for (int i=0; i< sizeB; i++ )
    {
        for (int j=0; j<sizeB; j++)
        {
            board[i][j] = '*';
        }
    }

}

void Gobang:: printBoard()
{
    for (int i=0;i<sizeB;i++)
    {
        for (int j=0;j<sizeB;j++)
        {
            cout << board[i][j];
        }
        cout << endl;
    }

}

void Gobang:: boardReset()
{
    for (int i=0;i<row;i++)
    {
        for (int j=0;j<cols;j++)
        {
            board[i][j] = '*';
        }
    }
}

void Gobang:: testFile()
{
    cout << sizeB << endl;
}
Last edited on
you forgot the [ /code ] (no spaces) closing tag. Can you also add indentation? ;D
I did..pleas help
Appears to be working for me... I got rid of input file and made it compile.

I think the problem was in your initialization method you might've been making an extra board instead of using the private board_ variable (inside Gobang class). Can't see because you didnt post your full class. Also I added underscores to what I assume are private variables, since this is common practice.

Edit: Also I see you're using rows, columns, *and* sizeboard? Why do you need sizeboard if you already have rows and columns?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
using namespace std;

class Gobang
{
public:
    Gobang()
    {
        board_ = NULL;
    }
    
    Gobang(int boardSize,int rows,int columns )
    {
        setParameters(boardSize,rows,columns);
    }
    
    ~Gobang()
    {
        board_ = NULL;
    }
    
    void setParameters(int boardSize,int rows,int columns)
    {
        rows_ = rows;
        sizeB_ = boardSize;
        cols_ = columns;
    }
    
    void initialization()
    {
        //Creation of Board in Dynamic memory
        board_ = new char*[rows_];
        for (int i=0; i< sizeB_; i++ )
        {
            board_[i] = new char[cols_];
        }
    
        for (int i=0; i<sizeB_; i++ )
        {
            for (int j=0; j<sizeB_; j++)
            {
                board_[i][j] = '*';
            }
        }
    
    }
    
    void printBoard()
    {
        for (int i=0; i<sizeB_; i++)
        {
            for (int j=0; j<sizeB_; j++)
            {
                cout << board_[i][j];
            }
            cout << endl;
        }
    
    }
    
    void boardReset()
    {
        for (int i=0; i<rows_; i++)
        {
            for (int j=0; j<cols_; j++)
            {
                board_[i][j] = '*';
            }
        }
    }
    
    void testFile()
    {
        cout << sizeB_ << endl;
    }
    
private:
    int rows_;
    int cols_;
    int sizeB_;
    char** board_;
};

int main()
{
    int sizeBoard = 15;
    //ifstream infile ("input.txt");
    //infile >> sizeBoard;
    int rows=15;
    int columns=15;
    Gobang game(sizeBoard,rows,columns);
    game.initialization();
    game.boardReset();
    game.printBoard();
    
    return 0;
}

In action: https://repl.it/repls/PristinePoorScriptinglanguages
Last edited on
Thanx bro..works perfectly! Only problem was that i had char** board = new char*[row] AND also had char**board in my private after removing the "Char **" in the initialization everything worked!! thanx man!!
Topic archived. No new replies allowed.