user3506463
user3506463

Reputation: 49

C++ copy constructor, assignment 'operator='

I'm trying to make a copy constructor or = operator. If I define a matrix named A, another as B and C and use the '=' operator as:

A=B

it performs well, However if i use like:

A=B+C i get this error: no matching function for '='.

The point is when I change the symbol(=) to (==) it works well,even in the case of A == B+C, however by using only the equality sign(=) it doesn't work! any ideas?

In the header file:

Simple2DMatrixD (const Simple2DMatrixD& matrixA)
{
   numRows = matrixA.numRows;
   numCols = matrixA.numCols;

   dataArray = new double[numRows * numCols];

   for (int iX = 0; iX < numRows; iX++)
   {
       for (int iY = 0; iY < numCols; iY++)
       {
           dataArray[(iX * numRows) + iY] = matrixA.getElement(iX,iY) ;
       }
   }
}


Simple2DMatrixD & assign (const Simple2DMatrixD & matrixB);
Simple2DMatrixD & sum (const Simple2DMatrixD & matrixA, const Simple2DMatrixD & matrixB);

// ADDITION OPERATOR

friend Simple2DMatrixD operator+ (Simple2DMatrixD & matrixA, Simple2DMatrixD & matrixB)
{
    Simple2DMatrixD matrixTemp(matrixA.numRows, matrixA.numCols);

    matrixTemp.sum(matrixA, matrixB);
    return (matrixTemp);
}

// ASSIGNMENT OPERATOR

Simple2DMatrixD & operator= (const Simple2DMatrixD & matrixB)
{
    this->assign(matrixB);
    return (*this);
}

// and in the source file:

Simple2DMatrixD & Simple2DMatrixD::assign (const Simple2DMatrixD & matrixB)
{

for (int r = 0; r < numRows; r++)
{
    for (int c = 0; c < numCols; c++)
    {
        this->setElement(r, c, matrixB.getElement(r, c));
    }
}
return (*this);

}

// MATRICES ADDITION

Simple2DMatrixD & Simple2DMatrixD::sum (const Simple2DMatrixD & matrixA, const Simple2DMatrixD &         
matrixB)
{

// TODO REPLACE WITH COMPAREDIMENSION FUNCTION
if ((this->numRows == matrixB.numRows)
    && (this->numCols == matrixB.numCols)
    )
{

    for (int r = 0; r < matrixA.numRows; r++)
    {
        for (int c = 0; c < matrixA.numCols; c++)
        {
            this->setElement(r, c, matrixA.getElement(r, c) + matrixB.getElement(r, c));
        }
    }
    return (*this);
}
else

{
    throw " Dimensions does not match!";
}

}

Upvotes: 2

Views: 301

Answers (2)

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42899

You are abusing friend specifier. You'll either have to declare the operator+ outside class's definition like this:

inline Simple2DMatrixD operator+(Simple2DMatrixD & matrixA, Simple2DMatrixD & matrixB)
{
  Simple2DMatrixD matrixTemp(matrixA.numRows, matrixA.numCols);

  matrixTemp.sum(matrixA, matrixB);
  return (matrixTemp);
}

And declare it as a friend in class Simple2DMatrixD:

class Simple2DMatrixD {
  friend Simple2DMatrixD operator+ (Simple2DMatrixD & matrixA, Simple2DMatrixD & matrixB);
  ...
};

Or you can declare it inside class's definition like this:

class Simple2DMatrixD {
  ...

  Simple2DMatrixD operator+(Simple2DMatrixD const &rhs)
  {
    Simple2DMatrixD matrixTemp(rhs.numRows, rhs.numCols);
    matrixTemp.sum(*this, rhs);
    return (matrixTemp);
  }

  ...
};

friend outside class's definition is an invalid specifier.

Upvotes: 1

vlad_tepesch
vlad_tepesch

Reputation: 6891

my intuition says me that your const correctness is faulty somewhere. please post the complete error message.

declare member function that does not change the object as const.

struct A
{
  float retunrsSomethingButDoesNotChangesA()const; // <- note the const
};

// in cpp:
float A::retunrsSomethingButDoesNotChangesA()const // <- note the const
{
  return 5.5f;
}

and make reference parameter of functions const if the function does not change them.

Upvotes: 0

Related Questions