dynamic multidimensional arrays in ctor
hello,
I need to create a dynamic 2D-array in a class. Here is my idea:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class GameOfLife {
private:
int** Matrix;
public:
GameOfLife(int rows, int cols);
~GameOfLife();
};
GameOfLife::GameOfLife(int r, int c) {
Matrix = new int[r];
for (int i=0; i<r; i++) {
Matrix[i] = new int[c];
}
}
GameOfLife::~GameOfLife(void) {
delete[] Matrix;
}
|
I get this message on line 9: "cannot convert 'int*' to 'int**' in assignment"
What does it means?
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
|
// 2D dynamic array
#include <iostream>
using namespace std;
void setVal(int **ar, const int r, const int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
ar[i][j] = i+j;
}
return;
}
int main() {
int row=3, column=5;
//cout << "enter row and column number:\n";
//cin >> row >> column;
int **arr = new int*[row];
for (int i = 0; i < row; ++i) {
arr[i] = new int[column];
}
setVal(arr, row, column);
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
cout << "arr[" << i << "][" << j << "]= " << arr[i][j] << " ";
}
cout << "\n";
}
for(int i=0; i<row; i++)
delete[] arr[i];
delete[] arr;
return 0;
}
|
Topic archived. No new replies allowed.