0
/*
unable to return value from following function due to if - else  block within function..*/
mat operator + (mat &m1,mat &m2) //mat is a class for matrix object
{
    int r1=m1.getrow(); //getrow returns number of rows in matrix
    int r2=m2.getrow();
    int c1=m1.getcol();  //getcol returns number of columns in matrix
    int c2=m2.getcol();
    if((r1!=r2)||(c1!=c2))
    {
        cout<<endl<<"Addition not possible.";

    }
    else
    {
        mat m3(r1,c1);
        for(int i=0;i<r1;i++)
        {
            for(int j=0;j<c1;j++)
            {
                m3.get_element(i,j,m1.get_element()+m2.get_element());
            }
        }
    }

}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Joatmon
0

My suggestion is that you return a reference to a matrix so you can do m1 + m2 + m3 etc etc.

Just add a & after mat in the function header.

As for your problem. this should be treated as an error whenever it called in my opinion, so I would assert that.

instead of the if statement:
assert((r1==r2)&&(c1==c2));

this requires #include <cassert>

If you don't want this treated as an error, what behavior do you desire for the addition of two matrices with unequal dimensions?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.