【发布时间】:2021-06-23 09:26:30
【问题描述】:
我创建了一个矩阵类如下:
template<typename T, unsigned N, unsigned M>
class Matrix {
public:
template <unsigned P>
Matrix<T,N,P> operator*(const Matrix<T,M,P>& other) const {
Matrix<T,N,P> result;
for(auto i = 0; i < N; i++) {
for(auto j = 0; j < P; j++) {
for(auto k = 0; k < M; k++) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
}
/* ... other members ... */
private:
std::array<std::array<T, M>, N> data;
};
我现在遇到的问题是我无法访问other.data,因为它是另一个类(其他维度)的私有成员。我尝试将operator* 声明为这样的非会员朋友:
template<typename T, unsigned N, unsigned M>
class Matrix {
public:
template <typename Type, unsigned RowA, unsigned ColA, unsigned ColB>
Matrix<Type, RowA, ColB> friend operator*(const Matrix<Type, RowA, ColA>& a, const Matrix<Type, RowA, ColB>& b);
/* ... */
};
template <typename Type, unsigned RowA, unsigned ColA, unsigned ColB>
Matrix<Type, RowA, ColB> operator*(const Matrix<Type, RowA, ColA>& a, const Matrix<Type, RowA, ColB>& b) {
Matrix<Type, RowA, ColB> result;
for(auto i = 0; i < RowA; i++) {
for(auto j = 0; j < ColB; j++) {
for(auto k = 0; k < ColA; k++) {
result.data[i][j] += a.data[i][k] * b.data[k][j];
}
}
}
return result;
}
但它不起作用 - 编译器抱怨它找不到 operator* 的有效重载。
我在这里还有哪些其他选择?解决方案是将所有具有兼容维度的矩阵声明为朋友,但这将是部分模板专业化,据我所知这是不可能的。像这样的:
template<typename T, unsigned N, unsigned M>
class Matrix {
public:
template<typename P>
friend class Matrix<T,P,N>;
/* ... */
};
我无法更改存储数据或Matrix 模板参数的方式 - 这是我被分配的任务。
【问题讨论】:
标签: c++ templates matrix operator-overloading