0

everyone:

Im creating a program that is able to create Matrices and and perform various operations on them for a course at school. They require us to overload the operators with with the appropriate Matrix Operations.

I am having a hard time with the following function:

typedef double matrixType;


using namespace std;


class Matrix{
protected:
    int m,n; // m:row size n:column size
    matrixType **a; //Allows us to acces the a(ij) i,j position of the matrix


//==================================================
// (==Operator)Verifies if two given Matrices are equal
//==================================================

bool Matrix::operator==(const Matrix &B){

bool flag=false;


if(B.m ==m && B.n ==n){

    for (int row=0; row<m; row++) {
        for (int col=0; col<n; col++) {
            if (B[row][col] != a[row][col]) {
                flag=false;
            }
        }
    }
    flag= true;
}

else{
    flag=false;

}

return flag;


}

Xcode warns me that at the following line:

 if (B[row][col] != a[row][col])

type 'const Matrix' doesn't provide a subscript operator

Note: Function Headers,constructors and other functions have been omitted from this code portion.

Any help would be greatly appreciated. Thank you.

3 Answers 3

5

Given your implementation, it should be if (B.a[row][col] != a[row][col])

BTW: You should read this page if you plan to implement your own matrix class.

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

1 Comment

He should probably also give it a subscript operator.
1

What you meant to do was

if (B.a[row][col] != a[row][col]) {
                flag=false;
}

And if you give it a moment of thought, couldn't you simply do

if (B.a[row][col] != a[row][col]) {
                return false;
}

and skip the flag variable altogeher? If yes, why, if not, why not (awards you bonus points ;).

If you want to really do B[][] you would have to implement operator[] for your class:

matrixType* operator[](size_t index) 
{
  return a[index];
}

2 Comments

The reason I was using a flag was because I was worried by only having return statements inside an IF structure. I did try to do it without a flag and got a Warning from my IDE: 'control may reach end of non-void function'
@edu222 Well yes, you should put another return statement at the end. So you need 3: 1. return false if B.m!=m&&B.n!=n 2. return false if the if fails (thats also called early-out, imagine a 10^10x10^10 matrix and all the unnecessary comparisions when they differ at index 0,0 ;) 3. return true at the end of the method.
1

Take a cue from boost::ublas::matrix, boost::gil::view_type, OpenCV::Mat, etc and use operator(int,int) for indexes instead of the subscript operator. It is worlds easier to implement, maintain, and debug.

1 Comment

HI, Thanks for the suggestion, I will check it as soon as I have some time. For now Ill finish my homework with the current implementation since I have it almost ready.:)

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.