Can't access a 2D array that I created in the constructor in the printMatrix class function. When I call the function in main with a simple cout << "test"; it will print that. If I try to print the values of matrixArray[][] it prints nothing and quits the program. Am I not referencing the 2d array correctly?
class matrix
{
int **matrixArray;
public:
int matrixSize = 0;
matrix(int matrixSize);
void printMatrix();
void makeMagicSquare();
};
matrix::matrix(int const matrixSize)
{
this->matrixSize = matrixSize ;
int** matrixArray = new int*[matrixSize];
for(int i = 0; i<matrixSize; i++){
matrixArray[i] = new int[matrixSize];
}
for(int row = 0; row < matrixSize ;row++)
{
for(int col = 0; col < matrixSize; col++)
{
matrixArray[row][col] =0;
cout << matrixArray[row][col] << " ";
}//End for Col
cout << endl;
}//End for Row
}
//printMatrix Function
void matrix::printMatrix(){
for(int row = 0; row < matrixSize;row++)
{
for(int col = 0; col < matrixSize; col++)
{
cout << "test" << " ";
//Not able to print from print function
cout << matrixArray[row][col] << endl;
}// end col
cout << endl;
}//end row
}
vector<vector<int> >instead?int **versusvector<vector>and all the memory problems it solves. I agree that 1D vectors are better, but you have to wrap them into an object or you constantly shoot yourself in the foot with index computations.int**:) Right about the indexing, but if you don't want to complicate (using a proxy class) one can re-defineoperator()(size_t, size_t)for indexing.