C++ 模板矩阵<;M、 N,类型>;c++;矩阵乘法
我在另一篇文章中得到了关于使用模板抽象类重写的帮助,多亏了社区,我才使它工作得很好。既然那篇文章的问题已经不存在了,我要问一个新问题: 我想实现乘法,所以我需要不同的模板参数使其工作(行数=列数条件),但我无法找到正确的方法C++ 模板矩阵<;M、 N,类型>;c++;矩阵乘法,c++,templates,C++,Templates,我在另一篇文章中得到了关于使用模板抽象类重写的帮助,多亏了社区,我才使它工作得很好。既然那篇文章的问题已经不存在了,我要问一个新问题: 我想实现乘法,所以我需要不同的模板参数使其工作(行数=列数条件),但我无法找到正确的方法 #pragma once #include "AbsMatrice.h" template <int M, int N, typename T> class Matrice : public AbsMatrice<M,N,T> { public
#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;
}
};
#pragma一次
#包括“AbsMatrice.h”
模板
类矩阵:公共矩阵
{
公众:
~Matrice(){}
//实施法
void print()常量重写
{
std::cout我看到了几个问题:
您的问题的答案是您不想返回*r
,您只想返回r
第二个问题是r
应该是Matrice
类型。因为它是当前矩阵的类型。我看到了几个问题:
您的问题的答案是您不想返回*r
,您只想返回r
第二个问题是,r
应该是Matrice
类型。因为它是当前矩阵的类型
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