Member functions are matched by their left argument, which is this pointer. Since native types cannot have member functions, you need to add the correct multiplication with custom types through functions other than members (as well as for other types that you do not have write access to).
template<typename T> Matrix<T> operator*(T const& scalar, Matrix<T> rhs) {
NOTE : I wrote above the non-member operator* , implemented in terms of the member operator*= . It is recommended that you write all the multiplications as non-member functions, and use the operator*= element to implement these multiplications using the Lhs matrix element.
This will: a) maintain a minimal class interface and b) prevent hidden conversions. For example. you can have a Matrix class that is implicitly converted to scalar if the sizes are 1x1, and these conversions can happen unexpectedly if you do not provide a separate overload, which is a direct match.
template<typename T> Matrix<T> operator*(Matrix<T> lhs, T const& scalar) { return lhs *= scalar; // calls lhs.operator*=(scalar); } template<typename T> Matrix<T> operator*(Matrix<T> lhs, Matrix<T> const& rhs) { return lhs *= rhs; // calls lhs.operator*=(rhs); }
Notice how the lhs matrix is a copy, not a link. This allows the compiler to do optimizations such as copy / elite semantics. Also note that the return type of these operators is Matrix<T> , not const Matrix<T> , which was recommended in some old C ++ books, but which prevents movement semantics in C ++ 11.
// class member template<typename T> Matrix<T>& Matrix<T>::operator*=(Matrix<T> const& rhs) { // your implementation return *this; } // class member template<typename T> Matrix<T>& Matrix<T>::operator*=(T const& scalar) { // your implementation return *this; }
TemplateRex
source share