Ah... friends... if you declare something inside a class to be a "friend", that means that it can access all the private and protected members of that class, but you knew that.
When I said take all the solo stuff and change it, I mean change this:
1 2 3 4 5 6 7 8
|
Matrix operator+(Matrix a) {
Matrix temp;
temp.a11=a11+a.a11;
temp.a12=a12+a.a12;
temp.a21=a21+a.a21;
temp.a22=a22+a.a22;
return temp;
}
| |
to this:
1 2 3 4 5 6 7 8
|
Matrix operator+(const Matrix &b, const Matrix &a) {
Matrix temp;
temp.a11=b.a11+a.a11;
temp.a12=b.a12+a.a12;
temp.a21=b.a21+a.a21;
temp.a22=b.a22+a.a22;
return temp;
}
| |
Notice that the a11, a12, a21, and a22 that were originally on their own now have a b. preceding them.
Now that you changed the implementation of your operator, go back to your class and look for its declaration there. As you see, it takes only one argument. If you try to compile your code without changing that line to
friend Matrix operator+(Matrix, Matrix);
, you'll get about 24 errors, because it's giving a different operator that you didn't implement friend privileges. Not good; you need to make sure it's the operator you just implemented outside the class. Copying the line I just gave you should fix it.
EDIT: I hope this was sufficient for an explanation, though if you want to know more about friends...
http://cplusplus.com/doc/tutorial/inheritance/
EDIT2: Added &s, and dealt with some const correctness.
-Albatross