I am trying to initialize a char** which will act as a two dimensional char array of size (board_size * board_size). However, I am having trouble filling the grid the character '-', when I get to that line I am getting exit code 11 which is a seg fault. How do I assign data to the dynamic 2d array. Is char ** the wrong type to use? What am I missing?
Code:
class World
{
public:
World(int num_ants, int num_doodlebugs, int board_size)
{
this->board_size = board_size;
this->num_ants = num_ants;
this->num_doodlebugs = num_doodlebugs;
this->board = new char*[board_size*board_size];
for(int i = 0; i < board_size; i++)
{
for(int j = 0; j < board_size; j++)
{
this->board[i][j] = '-';
}
}
cout << "Instantiated object" << endl;
};
void printWorld()
{
cout << "Printing World" << endl;
for(int i = 0; i < this->board_size; i++)
{
for(int j = 0; j < this->board_size; j++)
{
cout << this->board[i][j] << " ";
}
cout << endl;
}
}
private:
int num_ants;
int num_doodlebugs;
int board_size;
vector<Ant> ants;
vector<Doodlebug> doodblebugs;
char **board;
};