C++11 c++;11矩阵乘法

C++11 c++;11矩阵乘法,c++11,matrix,multiplication,C++11,Matrix,Multiplication,你好。我写了一个矩阵类,但我对不同维数的矩阵乘法有一些问题 template< typename T, size_t Row, size_t Col > class Matrix { public: ..................................... template< typename MulT > auto operator * (const MulT& other) -> Matrix<T, Row, other.getC

你好。我写了一个矩阵类,但我对不同维数的矩阵乘法有一些问题

template< typename T, size_t Row, size_t Col >
class Matrix
{
public:
.....................................
template< typename MulT >
auto operator * (const MulT& other) -> Matrix<T, Row, other.getColNum()>
{
    if (other.getRowNum() != getColNum())
        throw std::logic_error("Multiplication are not possible");

    Matrix<T, Row, other.getColNum()> temp;

    // Some operations.

    return temp; // Must return matrix with this type Matrix<T, Row, other.getColNum()>
                    // but it dont work.
}
.....................................
}; // class Matrix
模板
类矩阵
{
公众:
.....................................
模板
自动运算符*(常量和其他)->矩阵
{
if(other.getRowNum()!=getColNum())
抛出std::逻辑_错误(“不可能乘法”);
基质温度;
//一些行动。
return temp;//必须使用此类型矩阵返回矩阵
//但是它不起作用。
}
.....................................
}; // 类矩阵
这个代码不起作用。是否有可能解决此问题?

其他。getColNum()
可能不是
constepr
函数,因此不能用作模板非类型参数


阅读
constexpr
此处:

您不想在运行时检查if
if(other.getRowNum()!=getColNum())
,这应该在编译时完成。一种方法是仅为乘法有效时定义运算符。在这种情况下:

template< typename T, size_t Row, size_t Col >
class Matrix
{
public:
.....................................
template<size_t _RHSWIDTH>
Matrix<_T, _RHSWIDTH, Row> operator * (const Matrix<T, _RHSWIDTH, Col> &rhs) const
{
    Matrix<_T, _RHSWIDTH, Row> temp;

    // Some operations.

    return temp;
}
.....................................
}; // class Matrix
模板
类矩阵
{
公众:
.....................................
模板
矩阵运算符*(常数矩阵和rhs)常数
{
基质温度;
//一些行动。
返回温度;
}
.....................................
}; // 类矩阵

因此,任何无法相乘的矩阵相乘的尝试都将在编译时失败。作为一个完整的示例,我很久以前编写了一个完整的矩阵模板,它使用非常相似的语法:

那么什么不起作用呢?它不编译吗?它会编译但会使你的电脑过热吗?它会不会编译,然后把镇上所有的灯都调暗?你所做的就是说“这个垃圾不起作用”,然后粘贴你的代码。看在上帝的份上,表现出一些努力,像一个人一样描述你的问题。耶稣。@moo juice即使你说的话是可以理解的,请注意你的措辞。@embert-道歉-这些天来我有时会感到不努力。星期天快乐!:)