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;
}
Seb
  • 1,966
  • 2
  • 17
  • 32

4 Answers4

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 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 – Seb Feb 18 '14 at 04:34
  • @Foxic: This starts to sound like something prior to that in the file might be missing a semicolon, or something on that order. – Jerry Coffin Feb 18 '14 at 04:36
  • 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]) – Seb Feb 18 '14 at 04:48
  • @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](http://stackoverflow.com/a/2216055/179910). – Jerry Coffin Feb 18 '14 at 04:58
  • 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]? – Seb Feb 18 '14 at 05:04
  • @Foxic: Yes. Overload `operator()` instead (first example in the link I just posted). – Jerry Coffin Feb 18 '14 at 05:05
  • I feel like my head will explode, I just learned about overloading so I really have no idea how to do either. I think I'll make a new topic – Seb Feb 18 '14 at 05:09
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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
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.

ctschuster
  • 172
  • 3
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);
jia103
  • 1,116
  • 2
  • 13
  • 20