简体   繁体   English

C ++复制构造函数,赋值“ operator =”

[英]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和C,并使用'='运算符作为:

A=B A = B

it performs well, However if i use like: 它的性能很好,但是,如果我使用像:

A=B+C i get this error: no matching function for '='. A = B + C我收到此错误:'='没有匹配函数。

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! 关键是当我将符号(=)更改为(==)时,即使在A == B + C的情况下,它也能很好地工作,但是仅使用等号(=)则不起作用! 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!";
}

}

You are abusing friend specifier. 您正在滥用friend说明符。 You'll either have to declare the operator+ outside class's definition like this: 您必须像这样在类的外部声明operator+

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 : 并在类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. 类外部friend的定义是无效的说明符。

my intuition says me that your const correctness is faulty somewhere. 我的直觉告诉我,您的const正确性在某处存在错误。 please post the complete error message. 请发布完整的错误消息。

declare member function that does not change the object as const . 声明不将对象更改为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. 如果函数未更改,则使函数的引用参数为const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM