I was wondering how to go about this. I have a matrix object that is a 2d array that grabs a matrix. I am trying to build an add method to add my matrices together.I am a second semester student and have more methods but i am stuck on how to go about this. we have done up to overloading operators, dynamic memory and pointers. I just need some guidance to help me learn this stuff effectively instead of trying for 12 hours. Any help would be grateful.
my class function is
Matrix Matrix:: add(const Matrix & m) const
{
Matrix v; // I know to make a matrix to add
// I tried this but seems to not work
v=v[rows][cols]+ m[rows][cols];
return v
}
I think I have figured it out now. Thanks! But i will still vote best answer so have at it! It might be useful for other students.
I’m going to take a stab and assume your matrix class looks something like this:
If you want to add two Matrixes then you need some sort of function that takes two Matrixes and produces a third matrix such that each value in data is the addition of the two corresponding values in the original matrixes. One such function might look like this:
We’re going to assume that the matrixes are the same size and ignore the problems different sized matrixes cause. So all we do is loop through both matrixes and add the values to a new matrix. Now we can call code like:
You can see where this is going, all that’s left is to make it an operator, generally when you type
What the compiler actually see’s is
So all that’s left is to rename the add function to