Hello I have this problem..I realise I am trying to
I am calling function like this...a reference to E is passed in?
B = D.sum1(E);
MatrixClass MatrixClass::sum1(const MatrixClass &rhs) const//function to add elements of one MatrixClass to
{ //another and return the new MatrixClass
MatrixClass C;
if(this->dataptr == rhs.dataptr)
{
return *this;//check for self assignment
}
C.dataptr = new double[M * N];//allocate memory for C
if(M*N*sizeof(this->dataptr) == M*N*sizeof(rhs.dataptr))
{
for(int i = 0;i < (M*N);i++)
{
C.dataptr[i] = this->dataptr[i] + rhs.dataptr[i];
}
}
else
{
C.dataptr = 0;
cout << "MATRIX ARE NOT THE SAME DIMENSION";
}
return C;
}//end function sum1
This returns and calls the copy constructor passing a reference to D?
Copy constructor:
MatrixClass::MatrixClass(const MatrixClass &rhs)//copy constructor
{
M = rhs.M;
N = rhs.N;
this->dataptr = new double[M*N];
if(rhs.dataptr)
{
int num = sizeof(double);
memcpy(this->dataptr,rhs.dataptr,M*N*sizeof(double));
//for (int i = 0;i < 20;i++)
//this->dataptr[i] = rhs.dataptr[i];
}
else
{
this->dataptr = 0;
}
}//end copy constructor
The problem lies when I am trying to allocate memory to 'this->dataptr', I believe that 'this' is not initilized, but I thought that 'this' would refer to 'B' and 'rhs' would refer to 'D'??
Could anyone please help me to see where I am going wrong?
Thanks!