I want to overload operator of stream << for class CMatrix and there is popping out error
"Exception thrown: read access violation.m.matrix was 0xCDDDCDDE."
when it's trying to read m.matrix(debugging stops right there with error).
class CMatrix : IMatrix
{
protected:
uint32_t rows;
uint32_t columns;
double **matrix;
public:
CMatrix(uint32_t rows, uint32_t columns);
CMatrix(uint32_t rows, uint32_t columns, double **mat);
~CMatrix();
friend ostream& operator<< (ostream &stream, const CMatrix &m);
}
ostream & operator<<(ostream & stream, const CMatrix & m)
{
for (int i = 0; i < m.rows; i++)
{
for (int j = 0; j < m.columns; j++)
{
stream << m.matrix[i][j] << " ";
}
stream << endl;
}
return stream;
}
CMatrix::CMatrix(uint32_t rows, uint32_t columns, double * mat)
{
this->rows = rows;
this->columns = columns;
double** matrix = new double*[rows];
for (int i = 0; i<this->rows; i++)
{
matrix[i] = new double[columns];
memcpy(matrix[i], &mat[i * this->columns], sizeof(double)*this->columns);
}
}
CMatrix::~CMatrix()
{
for (int i = 0; i < this->rows; i++)
{
delete[] matrix[i];
}
matrix = NULL;
}