user1784297
user1784297

Reputation: 105

Operator += class overloading : parameter error

CPP-file:

Matrix2x2& operator +=(const Matrix2x2 & rhs)
{
    for (int i = 0; i < 2; i++)
    {
        for (int n = 0; n < 2; n++)
        {
            this->anArray.[i][n] += rhs.anArray[i][n];
        }
    }
    return *this;
}

Header-file:

class Matrix2x2
{
private:
    double anArray[2][2];
public:
    Matrix2x2();
    Matrix2x2(int a, int b, int c, int d);
    void setArrayValues(int row, int column, double value);
    const double getArrayValues(int row, int column) const;
    Matrix2x2& operator +=(const Matrix2x2 & rhs)
};

Main file:

Matrix2x2 aMatrix(4,4,4,4);
Matrix2x2 bMatrix;
aMatrix += bMatrix;

When I try to run this, I get this:

error: 'Matrix2x2& operator+=(const Matrix2x2&)' must take exactly two arguments

I can't see why?

I replaced it with

Matrix2x2& Matrix2x2::operator+=(const Matrix2x2 & rhs);

and then I got these errors:

error: extra qualification 'Matrix2x2::' on member 'operator+='

in the header file and

error: expected unqualified-id before '[' token|

in this line:

 this->anArray.[i][n] += rhs.anArray[i][n];

UPDATE2

I've shown you my class declaration in the header file, the invocation in the main file and the function definition in my cpp file. What else is there to show? I've currently corrected all that you pointed out, but I still get one of the same errors:

error: extra qualification 'Matrix2x2::' on member 'operator+=

in the cpp and header file.

Upvotes: 0

Views: 155

Answers (1)

riwalk
riwalk

Reputation: 14223

Ok, a large number of issues.

First, in your header file, this line needs to be within a the class declaration of Matrix2x2 (preferably under a public: label)

Matrix2x2& operator +=(const Matrix2x2 & rhs);

Second, you need to put your definition in the CPP file and it needs to look something like this:

Matrix2x2& Matrix2x2::operator+=(const Matrix2x2 & rhs)
{
    for (int i = 0; i < 2; i++)
    {
        for (int n = 0; n < 2; n++)
        {
            this->anArray[i][n] += rhs.anArray[i][n];
        }
    }
    return *this;
}

Third, you have an extra "." within your for loop. Change it from:

this->anArray.[i][n] += rhs.anArray[i][n];

to:

this->anArray[i][n] += rhs.anArray[i][n];

I can't guarantee that there aren't more issues. But those are the ones that I see based on what you've shown us.

Upvotes: 2

Related Questions