# 使用模板类进行矩阵乘法

##### 问题描述投票：0回答：1

``````template<class T, int rows, int cols>
class matrix {
private:
T m_data[rows * cols];
int m_rows;
int m_cols;
public:
//constructors
matrix() : m_cols(rows), m_rows(cols) {
for (int i = 0; i < rows * cols; i++)
m_data[i] = T(); //default constructor of data type
}

matrix(const matrix& other) : m_cols(other.m_cols), m_rows(other.m_rows) {
for (int i = 0; i < other.size(); i++)
m_data[i] = other.m_data[i];
}

//other functions...
``````

``````matrix<T, rows, int> operator*(const matrix<T, cols, int>& RHS) const { //error referenced below refers to this line
if (m_cols != RHS.m_rows) {
throw std::invalid_argument("Matrix dimensions must match for multiplication!");
}

matrix<T, rows, int> toReturn;

for (int i = 0; i < rows; ++i) {
for (int j = 0; j < RHS.m_cols; ++j) {
toReturn(i, j) = 0;
for (int k = 0; k < cols; ++k) {
toReturn(i, j) += (*this)(i, k) * RHS(k, j);
}
}
}

}
``````

``Error C2993 'int': is not a valid type for non-type template parameter 'cols'``

``main.cpp``

``````matrix<int, 2, 2> m1;
m1.set(0, 0, 1);
m1.set(0, 1, 2);
m1.set(1, 0, 3);
m1.set(1, 1, 4);

matrix<int, 2, 1> m2;
m2.set(0, 0, 1);
m2.set(1, 0, 1);

matrix<int, 2, 1> m3 = m1 * m2;
``````

``C2679 binary '*': no operator found which takes a right-hand operand of type 'matrix<int,2,1>' (or there is no acceptable conversion)``

c++ class templates matrix linear-algebra
##### 1个回答
0

``int``

``operator*``

``````template<class T, int rows, int dim, int cols>
matrix<T, rows, cols> operator*(const matrix<T, rows, dim>& m1, const matrix<T, dim, cols>& m2) {
matrix<T, rows, cols> res;
// calculation matrix elements of res
return res;
}
``````

``operator*``

``m_rows``
``m_cols``

``rows``
``cols``