0
/*
unable to return value from following function - adds two matrix class objects
*/
mat operator + (mat &m1,mat &m2)  // mat is a class for matrices
{
	int r1=m1.getrow(); // getrow returns number of rows
	int r2=m2.getrow();
	int c1=m1.getcol(); //getcol returns number of cols
	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());
			}
		}
	}

}
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

You need to create a new mat object inside that function, add the two matrixes parameters together then return it. I think this is a good candidate to use the throw statement if the two matrices can not be added.

mat operator + (mat &m1,mat &m2)  // mat is a class for matrices
{
	int r1=m1.getrow(); // getrow returns number of rows
	int r2=m2.getrow();
	int c1=m1.getcol(); //getcol returns number of cols
	int c2=m2.getcol();
mat m3(r1,c1); // <<<<<<<
	if((r1!=r2)||(c1!=c2))
	{
		cout<<endl<<"Addition not possible.";
throw; // <<<<  ?????		
	}
	else
	{
		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());
			}
		}
	}
  return m3; // <<<<<<<<<<<<<<<<<< here
}
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.