1

So I have a class called Cell, and I need to create a new Cell and put it into a 2d array. I believe the problem is how I created the 2D array. I have looked up how dynamic arrays work but I still can't find the problem. Below is some of my code and the first few errors I get

    Cell * board = new Cell[h]; //create new board
        for(int i = 0; i < h; i++){
            board[i] = new Cell[w];
            }
        for (int row = 0; row < h; row ++){     //initialize board
            for (int col = 0; col < w; col++){
                board[row][col] = new Cell; 
                board[row][col]->status = '#';
                board[row][col]->isCovered = true;
            }
        }

ERRORS:

minesweeper.h: In constructor ‘GameBoard::GameBoard(int, int, int)’:
minesweeper.h:29:17: error: no match for ‘operator=’ (operand types are 
‘Cell’ and ‘Cell*’)
    board[i] = new Cell[w];
             ^
minesweeper.h:29:17: note: candidate is:
minesweeper.h:4:8: note: Cell& Cell::operator=(const Cell&)
 struct Cell
    ^
minesweeper.h:4:8: note:   no known conversion for argument 1 from ‘Cell*’ 
to ‘const Cell&’
minesweeper.h:33:16: error: no match for ‘operator[]’ (operand types are 
‘Cell’ and ‘int’)
      board[row][col] = new Cell; 
            ^
minesweeper.h:34:16: error: no match for ‘operator[]’ (operand types are 
‘Cell’ and ‘int’)
      board[row][col]->status = '#';
                ^
2
  • You initialized the array for 1D instead 2D. Commented Dec 6, 2017 at 2:38
  • Before you attempt a 2D array of a custom class, try a 2D array of int. That way you can iron these problems out before you introduce a lot of new unknowns. Commented Dec 6, 2017 at 2:58

1 Answer 1

1

Change the line

Cell* board = new Cell[h]

to

Cell** board = new Cell*[h]

Basically you want to create a 2d array, so you need to create and array of Cell pointers (new Cell*[h]). Then for each of those Cell pointers you want to allocate memory for each individual Cell. This is done in the loop:

for(int i = 0; i < h; i++){
    board[i] = new Cell[w];
}
Sign up to request clarification or add additional context in comments.

4 Comments

this gives me the error: In file included from minesweeper.cpp:2:0: minesweeper.h: In constructor ‘GameBoard::GameBoard(int, int, int)’: minesweeper.h:27:30: error: cannot convert ‘Cell*’ to ‘Cell**’ in initialization Cell ** board = new Cell[h]; //create new board
That's because you didn't make the change properly. Read the post again.
Sorry about that! (running on very little sleep) New Error: 'minesweeper.h: In constructor ‘GameBoard::GameBoard(int, int, int)’: minesweeper.h:33:22: error: no match for ‘operator=’ (operand types are ‘Cell’ and ‘Cell*’) board[row][col] = new Cell; ^ '
You don't need that line. You've already created the Cell at that row and col in the loop above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.