Start New Discussion within our Software Development Community

Well I'm relatively new to the forums and to C++. I'd like to overload + and = to add two matrices by being able to use Z=X+Y format.

Header file:

#ifndef HOMEWORK1_H 
#define HOMEWORK1_H 

class Matrix{
	double **element;
	double getelement(int,int); 
	void setelement(int, int,double);
	Matrix & operator+(const Matrix &, const Matrix &);
	Matrix & operator=(const Matrix & );
	int r;
	int c;



Implementation code for operator+:

Matrix & Matrix::operator+( const Matrix&  s, const Matrix& t) //overloaded +
       Matrix temp(3,3);
        for (int r=0;r<3;r++)
            for (int c=0;c<3;c++)
				temp(r,c)=s.getelement(r,c)+ t.getelement(r,c)
		return temp;
Matrix & Matrix::operator=(const Matrix & q)
		Matrix temp(3,3);
        for (int r=0;r<3;r++)
            for (int c=0;c<3;c++)
		return *this;


I believe one problem I have is too many parameters for operator+, but the only example my professor gave me had it with two parameters. I cut the implementation file down to the basic lines.

Thanks for whatever help possible.

Matrix & operator+(const Matrix &, const Matrix &);

this is not the prototype for overloading binary operator.
The proper is

Matrix & operator+(const Matrix &);

The other operand for the operation is the calling object.


Matrix a, b, c;

c = a+b;

here the operator + is being invoked by the object a.
i.e. a.operator+(b)

U have to add the matrix b to a and return the sum which will get assigned to matrix c.

and what do expect the following statement to do???????????????

temp(r,c)=s.getelement(r,c)+ t.getelement(r,c)
This article has been dead for over six months. Start a new discussion instead.