Overloaded assignment and addition operator problem.

Hello,

This is my first time using this forum. I have ran into a hitch recently using some overloaded operators and cannot figure out why I am running into the following problem. When my overloaded "+" operator adds two of my objects, my overloaded "=" receives the address of my temporary object inside the definition of the "+" operator function. The + action is performed properly, and when only assigning a single object from main to another object from main, the code works properly. Here is a high level view of the problem.
In main:
1
2
3
4
5
6
7
8
	matrixType<int> matrix1;
	matrixType<int> matrix3;
        matrixType<int> matrix2;
.
.
       matrix2 = matrix1;   //works properly
       matrix3 = matrix1 + matrix2;        //does not work properly

Also, the action "+" is performed properly inside the function, but when the overloaded operator "=" takes the result of the "+" action, the values are destroyed.
Here is the definition file code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
template <class elemType>
class matrixType
{ 
public:
		matrixType<elemType>& operator+(const matrixType&) const;
		matrixType<elemType>& operator*(const matrixType&) const;
		matrixType<elemType>& operator=(const matrixType&);

		bool sizecomp(const matrixType&,char*) const;

		matrixType();
		matrixType(int,int);
		matrixType(matrixType&);
		~matrixType();

//protected:
	elemType matrix[5][5];
	dimension size;
};
template<class elemType>
matrixType<elemType>& matrixType<elemType>::operator +(const matrixType<elemType> &right) const
{
	matrixType<elemType> temp;
	int i,j;

	temp.size.columns = right.size.columns;
	temp.size.rows = right.size.rows;

	if (!sizecomp(right,"+"))
		return temp;
	for (i=0;i<size.rows;i++)
		for (j =0;j<size.columns;j++)
			temp.matrix[i][j] = matrix[i][j] + right.matrix[i][j];
	return temp;

}
matrixType<elemType>& matrixType<elemType>::operator =(const matrixType<elemType>& rightside) 
{
	if (matrix != rightside.matrix)
	{
		size.columns = rightside.size.columns;
		size.rows = rightside.size.rows;
		int i,j;


		for (i=0;i<size.rows;i++)
			for (j =0;j<rightside.size.columns;j++)
				matrix[i][j] = rightside.matrix[i][j];
	}

	return *this;
}


Thank you all for your help. I tried to only include the code that was relevant. If you need to see any more of it, please let me know and I can provide it.
You are returning references to temporaries in operator* and operator+.
Consider instead declaring the functions as:

1
2
3
4
5
6
7
template <class elemType>
class matrixType
{ 
public:
  matrixType<elemType> operator+(const matrixType&) const;
  matrixType<elemType> operator*(const matrixType&) const;
};


And your code should work. Note that operator= is declared correctly,
however you can't simply compare the matrices as you are doing.
Change the if() to

 
if( this != &rhs ) { ... }


Topic archived. No new replies allowed.