Matrix class - overloading addition operator

Solved
Last edited on
It was too literally painful reading through the unformatted, unindented code (yes, literally, I now have a headache and eye strain). So I gave up half way through, nor is there any guarantee I've read things properly, so...

in the overloaded operators you are returning a reference to a temporary, so if this worked for you, you were (un)lucky. Since your operators are member variables, making them const is pointless, this means your use of temp is also unnecessary. The purpose of an operator is to operate. If you don't want it to change the object use a global operator that accepts const references instead. Also, you can't synthesize operator+= or -=, if + and - are const. If you insist on doing it this way, you'll need to return an object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
matrix& operator+(const matrix&);// Overload +
...
//EDIT: since I moaned about indentation as well...
//Overload +
matrix& matrix::operator+(const matrix& m) 
{
	if ((rows==m.get_rows()) && (columns==m.get_columns())) 
	{
		const int Size=m.get_rows()*m.get_columns();
		for (int i=0; i<Size; i++) 
		{
			mdata[i]+=m.mdata[i];
		}
	
		return *this;
	}
	else 
	{
		cout<<"Error: Matrices must have the same dimensions."<<endl;
	}
}
Last edited on
Sorry - I didn't know how to paste in my formatted/indented code. I will learn how to next time I ask a question here.

Thanks so much for your help, that's worked!

I'm not sure I understand your explanation about not needing const before the function definition (forgive my naivety!). Could you explain it again please?
For a decent tutorial, see:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

Check out volume 1 of the book, chapter 8 constants.

You may also want to try 11 (copy constructor) and 12 (operator overloading)
Thanks!

And yeah I'll definitely need to look at the copy constructor section, my function for overloading >> throws all sorts of errors


When I do cout<<temp;, the output is fine. However, if I write m3=m1*m2 in the main program, it leads to a crash.

I suspect this is a C++ thing rather than a matrix calculation gone wrong?
Last edited on
operator* should not affect this - that is, it should be suffixed with const. What you have written is better suited as an implementation of operator*=. operator* on the other hand, returns a third matrix without modifying either of the inputs.
I'm not sure I understand what you mean. Also, I've noticed that if I call m1+m2, it alters the values of m1. What's going on?
Read my post more carefully.

+= and *= should modify their left hand side.
+ and * should not.

You wrote the code for + and * as if you were writing the code for += and *=
Right, i see. But I thought for the * overloading I'd defined a temporary matric to store the multiplication and then returned it. Why doesn't that work?
Because operator functions are just like any other function. When you write a + b, it is the same as a.operator+(b) - in fact the latter is actually valid syntax.

If you want to avoid confusion, overload operators as non-member functions (that is, don't define them inside the class):
1
2
3
4
blah operator+(blah const &left, blah const &right)
{
    //...
}
But making them non member functions seems pointless as they'd have to be friends of the matrix class anyway.

I don't get why declaring a temporary matrix and returning that didn't work
Sorry, I misread your code - ignore most of what I've said.

You can't return a reference to a temporary object, you need to return it by value. It's as simple as removing an ampersand.
Topic archived. No new replies allowed.