我有一个不同的帖子来获取关于覆盖模板抽象类的帮助,并且由于社区,我得到了完美的工作。由于该帖子的问题已经消失,我问了一个新问题:
我想实现乘法,所以我需要不同的模板参数才能使它工作(行数=列数条件),但我无法弄清楚正确的方法。
#pragma once
#include "AbsMatrice.h"
template <int M, int N, typename T>
class Matrice : public AbsMatrice<M,N,T>
{
public:
~Matrice(){}
//implementation de l'affichage
void print() const override
{
std::cout << "Matrice :" << std::endl;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
std::cout << " " << this->m_data[i][j] << " ";
}
std::cout << std::endl;
}
}
template<int O>
Matrice<M, O, T>& mul(Matrice<N, O, T> &a)
{
Matrice<M,O,T> r;
T sum;
for (int c = 0; c < M; c++) {
for (int d = 0; d < O; d++) {
for (int k = 0; k < N; k++) {
sum = sum + m_data[c][k] * a(k,d);
}
r(c,d) = sum;
sum = 0;
}
}
return r;
}
};
使用mul函数时出现错误:
error C2100: illegal indirection
see reference to function template instantiation 'Matrice<2,2,int> &Matrice<2,2,int>::mul<2>(Matrice<2,2,int> &)' being compiled
谢谢你的帮助!
PS :()运算符工作正常,所有不可见的代码工作得很好,我正在努力使用这个函数。
答案 0 :(得分:0)
我看到了几个问题:
回答你的问题是,你不想返回*r
,只想回复r
。
第二个问题是r
应该是Matrice<M,O,T>
类型。因为它是当前矩阵的类型。