1

The object of the class 'Square_Matrix' stores an integer matrix.

Is this how I can check if two matrices are equivalent? I want to compare two 2d arrays by overloading '==' to check if every element is identical between the 2 matrices.

E.g: I need the following to work:

Square_Matrix a,b;
if (a == b) {return 1;}

Both a and b are dynamic 2d arrays.

However, I'm getting an error: unexpected initializer before 'operator'. How can I fix this? Other than that, is this how the syntax should look like?

//header file
bool operator==(Square_Matrix array1, Square_Matrix array2);

//.cpp file
bool Square_Matrix operator ==(Square_Matrix array1, Square_Matrix array2){
    if (array1.size != array2.size){
        return false;
    }
    for (int i = 0; i < array1.size; i++){
        for (int j = 0; j < array1.size; j++){
            if (array1[i][j] != array2[i][j]){
                return false;
            }
        }
    }
    return true;
}

4 Answers 4

3

Most binary operators can be overloaded in either of two different ways. One is as a member function, the other as a global (free) function.

The member function version will take one parameter. An expression like: x == y will be interpreted as x.operator==(y).

The free function version takes two parameters. An expression like x == y is interpreted as operator==(x, y).

You need to decide which of those you're going to use, and define the number of parameters appropriately. Right now, it looks like you have a free function taking only one parameter, which would work for a unary operator, but not a binary operator.

When you overload as a free function, it's normally to provide symmetry. Specifically, a free function can convert either the left or the right operand to the correct type for the operator. A member function overload can only convert the right operand to the correct type for the operator.

Sign up to request clarification or add additional context in comments.

7 Comments

I edited my code above to work as operator==(x, y). Does it seem fine? Now I'm getting the error Square_Matrix::operator== must take one argument, but I changed it in both the header and .cpp file
@Foxic: This starts to sound like something prior to that in the file might be missing a semicolon, or something on that order.
Now I'm getting "error: no match for operator[] (operand types are 'Square_Matrix' and 'int') which points to line //if (array1[i][j] != array2[i][j])
@Foxic: operator[] can only take a single operand, so if you really want to use array1[i][j] notation for your square-matrix type, you need to overload Square_matrix::operator[] to return a proxy type that itself overloads operator[]. 3D example.
I'm still pretty new at coding, it's pretty difficult to understand that. Is there any alternative I can use instead of array1[i][j]?
|
2

The operator should be declared as

bool operator ==( const Square_Matrix &array2 ) const;

if it is a member of the class.

Or it could be declared as a friend function of the class as

friend  bool operator ==( const Square_Matrix &array1, const Square_Matrix &array2 );

I suppose that the class has data member with name size that contains the size of the square matrix

bool Square_Matrix::operator ==( const Square_Matrix &array2 ) const
{
    if ( size != array2.size ) return false;

    for (int i = 0; i < size; i++ )
    {
        for ( int j = 0; j < size; j++ )
        {
            if ( mPoint[i][j] != array2.mPoint[i][j] ) return false;
        }
    }

    return true;
}

EDIT: I removed some typos.

Comments

1

The problem with your code is that you need a ::

bool Square_Matrix operator ==(Square_Matrix array2){

becomes this:

bool Square_Matrix::operator ==(Square_Matrix array2){

The error "unexpected initializer before 'operator'" is because it didn't understand you were trying to use a class method.

Comments

0

Change Square_Matrix operator == to Square_Matrix::operator ==.

Also, don't you want to pass Square_Matrix by reference into your operator?

bool operator==(const Square_Matrix& that) const;

or

static bool operator==(const Square_Matrix& a,
                       const Square_Matrix& b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.