-
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];
}
-
Stop. Have you considered the use of a std::vector instead of manual memory management of a dynamic array?
-
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()?
-
Assuming this is some exercise (use vectors as laserlight says, if this is real code), your member variable needs to be
-
Thanks for advice.
I am doing R&D with c++ that's why i asked this question.
Please help me to solve this
-
:) Thanks,
I was really missing this part int **p