Hello could someone explain what I'm doing wrong to get the error: Segmentation fault: 11 when trying to fill in a 2D array from two for loops?
Here is the code:
Matrix.h
#ifndef _Matrix_h
#define _Matrix_h
using namespace std;
class Matrix
{
public:
Matrix(); // Constructor
Matrix(int M, int N);
double getRead();
protected:
double **matrix;
int rows;
int columns;
};
#endif
Matrix.cpp
#include <iostream>
#include "Matrix.h"
using namespace std;
Matrix::Matrix(){};
Matrix::Matrix(int M, int N)
{
rows = M;
columns = N;
double **matrix = new double*[rows*sizeof(double*)];
for(int i = 0; i < rows; i++)
matrix[i] = new double[columns]; // allocate columns for each row
for(int i=0, j=0; (i < rows, j < columns); i++, j++)
{
matrix[i][0] = i;
matrix[0][j] = j;
}
}
double Matrix::getRead()
{
for(int i=0, j=0; (i < rows, j < columns); i++, j++)
{
cout << matrix[i][j] << endl;
}
}
main.cpp
#include <iostream>
#include "Matrix.h"
using namespace std;
void readMatrix();
int main()
{
Matrix m(5, 5);
m.getRead();
return 0;
}
Any help would be greatly appricated :)