# dynamic 2d-array

• 10-18-2012
sujitkashyap
dynamic 2d-array
Hi All,

While running the below code i am getting
Error like :-

In member function ‘void Matrix::get_element(int, int, int)’:
cons_2d_array.cpp:13:9: error: invalid types ‘int[int]’ for array subscript
cons_2d_array.cpp: In member function ‘int& Matrix: put_element(int, int)’:
cons_2d_array.cpp:16:23: error: invalid types ‘int[int]’ for array subscript
cons_2d_array.cpp: In constructor ‘Matrix::Matrix(int, int)’:
cons_2d_array.cpp:23:18: error: cannot convert ‘int**’ to ‘int*’ in assignment
cons_2d_array.cpp:25:28: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]

Code:

``` class Matrix {         int *p;         int d1,d2;     public:         Matrix()         {d1=0;d2=0;}         Matrix(int x,int y);         void get_element(int i, int j, int value)         {p[i][j]=value;}         int & put_element(int i, int j)         {return p[i][j];} }; Matrix :: Matrix(const int x, const int y) {         d1=x;         d2=y;         p = new int *[d1];         for(int i=0; i<d1; i++)           p[i]= new int[d2]; }```
• 10-18-2012
laserlight
Stop. Have you considered the use of a std::vector instead of manual memory management of a dynamic array?
• 10-18-2012
brewbuck
So, if I want to get the value of an element, I use put_element(), and if I want to set the value I use get_element()?
• 10-18-2012
Salem
Assuming this is some exercise (use vectors as laserlight says, if this is real code), your member variable needs to be

Code:

`        int **p;`
• 10-20-2012
sujitkashyap