-1

So this is my class

    class Map {
        Field*** f;
        int rows;
        int columns;
    };

How can I make a matrix of pointers to the class Field? I tried this but it doesnt work.

    Map(int rows_, int columns_) : rows(rows_), columns(columns_) {
        f = new Field*[][];
        *f = new Field*[rows];
        for (int i = 0; i < rows; i++) {
            *f[i] = new Field[columns];
        }
    }
dstan
  • 29
  • 5
  • 1
    `f = new Field*[][];` -- What is that supposed to do? You need to specify how many *pointer-to-pointers* there will be. The compiler cannot guess this amount -- you need to know what it is. Also [here is a link to a 3D matrix](https://stackoverflow.com/questions/52068410/allocating-a-large-memory-block-in-c/52069368#52069368) creation using triple stars (not a good thing to do, IMO). – PaulMcKenzie Dec 07 '19 at 03:18

1 Answers1

0

You may consider rethinking about what you are doing. A flattened one dimensional array is a better choice.

If you still want to a *** pointer here is how you can make one:

f = new Field**[Row];
for(int i =0; i<Row; i++){
  f[i] = new Field*[Col];
  for(int j =0; j<Col; j++){
    f[i][j] = new Field[H];
    for(int k = 0; k<H;k++){
      f3D[i][j][k] = 0;
    }
  }
}

Don't forget to free it.

Oblivion
  • 7,176
  • 2
  • 14
  • 33