I came across a problem with my matrix class and i can't find a solution.
Matrix = ROWSxCOLUMNS
Let A be a 3x4 matrix
Let B be a 4x5 matrix
The operation AxB (which is defined only if the columns of A match the rows of B) leads to a 3x5 matrix.
I wanted to create a templated class that does just that.
Matrix<int,3,4> A;
Matrix<int,4,5> B;
Matrix<int,3,5> matrix = A*B;
My code:
template <class T, unsigned int ROWS, unsigned int COLUMNS>
class Matrix {
public:
/* blabla */
const Matrix<T, ROWS, /* ? */ >&
operator*(const Matrix<T, COLUMNS, /* ? */ >& matrix) const
{
/* multiplication */
}
/* blabla */
};
I don't know what to insert in the /* ? */ sections.
Is there a way to let the compiler accept any unsigned integer value? Should i rewrite the code in a different way?
Note: I'm creating this class for academic purposes, i don't care if there are already libraries that do this.
Use a template member function with an integer template parameter:
template<class T, unsigned int ROWS, unsigned int COLUMNS>
class Matrix {
public:
...
template<unsigned int N>
Matrix<T, ROWS, N>
operator*(const Matrix<T, COLUMNS, N>& matrix) const
{
/* multiplication */
}
...
};
Also, do not return the value by reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With