Reputation: 21
template <size_t M, size_t N, typename T>
class Matrix
{
public:
Matrix<M, N, T> operator +(const Matrix<M, N, T>& B) const;
template <size_t P> Matrix<M,P,T> operator*(const Matrix<N, P, T>& B) const;
template <typename T2> operator T2() const;
private:
T __x[M][N];
};
The body has written fine, and everything works well. When I define two Matrices as below:
Matrix < 10, 10, int> m1;
Matrix < 10, 10, float> m2;
m1 + m2; // OK
m1 * m2; // error: no match for 'operator*' in 'm1 * m2'
The addition works well, because an implicit casting has performed on it, but for the multiplication of different value types, an error occurs.
error: no match for 'operator*' in 'm1 * m2'
Any idea ?!
Upvotes: 2
Views: 135
Reputation: 76788
This question has a similar problem. The reason for you error is that implicit conversions are not considered when deducing template arguments. Since your multiplication operator is a function-template, and you call without explicitly providing the parameter, the compiler tries to deduce the argument type and fails. To demonstrate, if you explicitly provide the parameter P
, it compiles:
m1.operator*<10>(m2);
To fix the problem, you could make the value-type of the right-hand-side a template-argument too:
template <size_t P, typenmame T2>
Matrix<M,P,T> operator*(const Matrix<N, P, T2>& B) const;
Upvotes: 2