printMatrix 클래스 함수의 생성자에서 만든 2D 배열에 액세스 할 수 없습니다. 내가 간단한 cout으로 main에서 함수를 호출 할 때 < < "test"; 그것을 인쇄합니다. matrixArray [] []의 값을 인쇄하려고하면 아무 것도 인쇄되지 않고 프로그램이 종료됩니다. 2d 배열을 올바르게 참조하지 않습니까?C++의 클래스 함수에서 2 차원 배열 참조
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
}
실제로 행렬을 1 차원 벡터에 저장해야합니다. – NathanOliver
대신에 '벡터>'을 사용하면 누군가가 당신을 때릴 것입니까? –
@ Jean-FrançoisFabre 캐시 지역에 대해 많은 관심을 가진 사람 만. 1D 벡터가 더 좋습니다. 바로 그것에 대해 – vsoftco