I have been having a lot of trouble getting my overloaded operators to properly with each other, especially in cases such as a=a+b+c
When I execute the program, I get an error: "Unhandled exception at 0x01201f10 in Project1_v3.exe: 0xC0000005: Access violation reading location 0xcccccccc." But I am not sure exactly what that means or where it comes from.
I have posted all relevant code including my test file. I would really appreciate any insights you have about what could be causing this problem.
Implementation file:
#include "matrix.h"
//Constructors
matrix::matrix(int r, int c)
//create matrix with specified row and column dimensions
{
try
{
if ((r<=0)||(c<=0))
throw invalid_argument ("Matrix row and column arguments must be greater than zero.");
row=r;
column=c;
}
catch (exception & ex)
{
cout<<"Error: "<<ex.what()<<endl;
}
try
{
data=new matrixData * [row];
for (int i=0; i<row; i++)
data[i]=new int [column];
}
catch (exception & ex)
{
cout<<"Error: "<<ex.what()<<endl;
}
fillMatrix();
}
//Destructor
matrix::~matrix ()
{
for (int i=0; i<row; i++)
delete [] data[i];
delete [] data;
}
matrix & matrix::operator=( const matrix & in)
{
if ((row!=in.getRows())||(column!=in.getColumns()));
{
column=in.getColumns();
row=in.getRows();
}
if (!(*this==in))
{
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
data[i][j]=in.getEntry(i, j);
}
}
return *this;
}
matrix & matrix::operator+=(const matrix & in)
{
try
{
if ((column!=in.getColumns())||(row!=in.getRows()))
throw invalid_argument ("Matricies must be of equal size.");
}
catch (exception & ex)
{
cout<<"Error: "<<ex.what()<<endl;
return *this;
}
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
this->setEntry(i,j,((data[i][j])+in.getEntry(i,j)));
}
return *this;
}
matrix & matrix::operator+(const matrix & in)
//Computes and returns the sum of two matricies
{
return matrix (*this) += in;
}
ostream& operator<<(ostream& output, const matrix & in)
//print matrix to standard output
{
for (int i=0; i<in.getRows(); i++)
{
output<<endl;
for (int j=0; j<in.getColumns(); j++)
{
output.width(3);
output<<in.getEntry(i,j)<<" ";
}
}
output<<endl;
return output;
}
//Helper Functions used to implement matrix class member functions
void matrix::fillMatrix()
{
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
data[i][j]=(matrixData) i-j;
}
}
int matrix::getColumns()const
{
return column;
}
int matrix::getRows() const
{
return row;
}
int matrix::getEntry(const int r_index, const int c_index) const
{
return data[r_index][c_index];
}
void matrix::setEntry (const int r_index, const int c_index, const matrixData value)
{
data[r_index][c_index]=value;
}
Test File:
int main()
{
matrix a(3, 4);
matrix b(3, 4);
matrix c(4, 4);
matrix d(4, 4);
matrix e(4, 4);
matrix f(4, 4);
matrix h(4, 4);
if ( a != b )
{
cout << "Matrix a and b are not equal" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
};
e += f;
cout << "f = " << f << endl;
cout << "e -= f = " << e << endl;
f = e = d;
h = d + e + f; //error.
cout << "d = " << d << endl;
cout << "e = " << e << endl;
cout << "f = " << f << endl;
cout << "h = " << h << endl;
if ( h == e ) cout << "Matrix h and e are the same" << endl;
else
{
cout << "Matrix h and e are not equal" << endl;
cout << "h = " << h << endl;
cout << "e = " << e << endl;
}
return 0;
}