Q:

Overloading operator in class

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;
}

A:

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.

