【发布时间】:2012-01-25 02:23:50
【问题描述】:
我有一个如下的矩阵类:
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 data[M][N];
};
// ... the body is in header file too ...//
正文写得很好,一切都很好。 当我定义两个矩阵如下:
Matrix < 10, 10, int> m1;
Matrix < 10, 10, float> m2;
m1 + m2; // OK
m1 * m2; // error: no match for 'operator*' in 'm1 * m2'
第一个“+”运算符运行良好,因为对其执行了隐式转换。 但是对于不同值类型的第二个'*'运算符,会发生错误。
错误:'m1 * m2' 中的 'operator*' 没有匹配项
有什么想法吗?!
更新: 所有代码都在头文件中。除了 '*' 运算符,我没有问题。
关于“+”运算符你能说什么?我知道关于模板/运算符/转换的一切......但是这个问题就像我的 gcc 编译器的错误!?我写了一个cast-operator,这个运算符在'+'运算符之前调用,但我不知道为什么它不能为'*'运算符执行!
【问题讨论】:
标签: c++ templates casting operators