简体   繁体   English

模板矩阵类中的operator *的多重重载

[英]Multi-overloading of operator* in template matrix class

I have a template class of myMatrix mainly like: 我有一个myMatrix的模板类,主要是:

template<class T, int row, int col>
class myMatrix{
   T *_mat;
public:
   template<int r, int c> using matrix_t = T[r][c];
   myMatrix(const matrix_t<row, col> &);
   myMatrix(const myMatrix<T, row, col> &);
   myMatrix &operator=( const myMatrix<T, row, col>& );
   //...
   // next is the part of * overloading
   myMatrix<T, row, col> operator*(const T& scalar);
   typename< int c2 >
   myMatrix<T, row, c2> operator*( const myMatrix<T, col, c2>& );
   typename< int c2 > 
   myMatrix<T, row, c2> operator*( const matrix_t<col, c2>& );
   typename< int r2 > 
   friend myMatrix<T, r2, col> operator*( const matrix_t<r2, row>&, const myMatrix<T, row, col>& );
   // ...
};

Then I design another class Reference : 然后,我设计另一个类Reference

template<int dim, int loop>
class Reference{
    myMatrix<int, dim, loop> _matA;
    myMatrix<int, dim, 1> _matC;
public:
    Reference(const myMatrix<int, dim, loop> &, const Matrix<int, dim, 1> &);
    Reference(const Reference<dim, loop> &);
    Reference<dim, loop> &operator=(const Reference<dim, loop> &);
    // ...
    friend Reference<1, loop> operator*(const Matrix<int, 1, dim> &alpha,
           const Reference<dim, loop> & ref)
    {
       return Reference(alpha * ref._matA, alpha * ref._matC);  // **Problem!!!**
    }
    // ...
};

When I test the codes with something like 当我用类似的东西测试代码

    const int matA[3][3] = {1,2,3,4,5,6,7};
    const int matC[3][1] = {1,2,3};
    const int alp[][3] = {1,2,2};
    Reference<3,3> ref(matA, matC);
    Matrix<int, 1, 3> alpha(alp);
   // Reference<1,3> ref_t = alp * ref;
    Reference<1,3> ref_t = alpha * ref; // **can not compile!!!**

The problem occurs: 出现问题:

binary '*': no operator found which takes a left-hand operand of type 'const myMatrix' ( or there is no acceptable conversion )... 二进制'*':没有找到采用'const myMatrix'类型的左操作数的运算符(或没有可接受的转换)...

Then here come my questions: 然后是我的问题:

  1. Of all the +4 overloadings in class myMatrix , if there are any redundancies? myMatrix类中的所有+4重载中,是否存在任何冗余?

  2. Maybe just the overloading version of typename< int c2 > myMatrix<T, row, c2> operator*( const myMatrix<T, col, c2>& ); 也许只是typename< int c2 > myMatrix<T, row, c2> operator*( const myMatrix<T, col, c2>& );的重载版本typename< int c2 > myMatrix<T, row, c2> operator*( const myMatrix<T, col, c2>& ); can service the following two overloads since a built-in 2d-array like arr[][] can convert to a myMatrix due to my constructor myMatrix(const matrix_t<row, col> &); 由于built-in 2d-array like arr[][]构造函数myMatrix(const matrix_t<row, col> &); built-in 2d-array like arr[][]built-in 2d-array like arr[][]可以转换为myMatrix因此可以处理以下两个重载myMatrix(const matrix_t<row, col> &); ?

  3. What is the reason for the compiling error? 编译错误的原因是什么?

The compiler error says what you've done wrong: 编译器错误指出您做错了什么:

binary '*': no operator found which takes a left-hand operand of type 'const myMatrix' 二进制'*':未找到采用'const myMatrix'类型的左操作数的运算符

You have 你有

operator*(const myMatrix&);

but no 但不是

operator*(const myMatrix&) const;

So alpha * ref can't match (because alpha is const& ). 所以alpha * ref无法匹配(因为alphaconst& )。

The usual way to implement T::operator* is 实现T::operator*的通常方法是

T& operator*=(const T&);
T operator*(const T&) const;

Or (better), make the * operator a free function with two arguments (this can work better if you might need to promote the left-hand argument). 或者(更好),使*运算符成为带有两个参数的自由函数(如果您可能需要提升左手参数,则可以更好地工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM