mardi 12 octobre 2021

"terminate called recursively" error when initialising a 2d array in C++

I have been trying to program a Sudoku game in C++11. I have a Cell class which is used as a single square in a sudoku grid. The grid is encapsulated in the Sudoku class.

Here is the Sudoku class constructor:

Sudoku::Sudoku(int rows, int cols)
{
    this->setRows(rows);
    this->setCols(cols);

    this->grid = new Cell **[this->rows];

    unsigned int i, j;

    for (i = 0; i < this->rows; i++)
    {
        for (j = 0; j < this->cols; j++)
        {
            std::cout << "i: " << i << " j: " << j << std::endl;
            this->grid[i][j] = new Cell();
        }
    }

    this->populateGrid();
    this->hideCells();
}

And here is the Cell constructor:

Cell::Cell()
{

    this->number = 0;

    this->isHidden = false;
}

because of the print statement in the Sudoku constructor, I get the following output:

i: 0 j: 0
i: 0 j: 1
i: 0 j: 2
terminate called recursively

Important note: the Sudoku grid is initialised to be 10x10 cells. So I really don't know why it stops after creating the first 3 cells.

Thanks in advance to anyone who answers.

Aucun commentaire:

Enregistrer un commentaire