C++ 模板矩阵乘法c++;

C++ 模板矩阵乘法c++;,c++,C++,我试图用模板做矩阵乘法,但我一直得到以下错误。(我正在尝试将非平方矩阵相乘) 错误1错误C2593:“运算符*”不明确 有谁能给我一个如何解决这个问题的建议吗 //Matrix.h #pragma once #include <iostream> #include <vector> using namespace std; template<class T, int m, int n> class Matrix; template<class T,

我试图用模板做矩阵乘法,但我一直得到以下错误。(我正在尝试将非平方矩阵相乘)

错误1错误C2593:“运算符*”不明确

有谁能给我一个如何解决这个问题的建议吗

//Matrix.h
#pragma once
#include <iostream>
#include <vector>
using namespace std;

template<class T, int m, int n>
class Matrix;

template<class T, int m, int n, int l>
Matrix<T, m, n> operator*(const Matrix<T, m, n>&, const Matrix<T, n, l>&);

template<class T, int m, int n>
class Matrix
{
vector<vector<T>> elements;
int nrow;
int ncol;

public:
    Matrix();
    ~Matrix();
    void print();
    template<int l>
    friend Matrix<T, m, l> operator*<>(const Matrix<T, m, n>&, const Matrix<T, n, l>&);

};

template<class T, int m, int n>
Matrix<T, m, n>::Matrix() : nrow(m), ncol(n)
{
    for (int i = 0; i < nrow; i++){
        vector<T> row(ncol, i);
        elements.push_back(row);
    }
}

template<class T, int m, int n>
Matrix<T, m, n>::~Matrix(){}

template<class T, int m, int n>
void Matrix<T, m, n>::print()
{
    for (int i = 0; i < nrow; i++){
        for (int j = 0; j < ncol; j++){
            cout << elements[i][j] << " ";
        }
    cout << endl;
    }
}

template<class T, int m, int n, int l> 
Matrix<T, m, l> operator*(const Matrix<T, m, n>& m1, const Matrix<T, n, l>& m2){
    int nrow = m1.nrow;
    int ncol = m2.ncol;
    Matrix<T, m, l> m3;
    for (int i = 0; i < nrow; ++i){
        for (int j = 0; j < ncol; ++j){
            m3.elements[i][j] = 0;
            for (int k = 0; k < m1.ncol; k++){
                T temp = m1.elements[i][k] * m2.elements[k][j];
                m3.elements[i][j] = temp + m3.elements[i][j];
            }
        }
    }
return m3;
}

//main.cpp
#include "Matrix.h"
using namespace std;

int main()
{
Matrix<int, 3, 2> a;
Matrix<int, 2, 1> b;
Matrix<int, 3, 1> c;
c = a*b;

c.print();
}
//Matrix.h
#布拉格语一次
#包括
#包括
使用名称空间std;
模板
类矩阵;
模板
矩阵算子*(常数矩阵&,常数矩阵&);
模板
类矩阵
{
矢量元素;
int nrow;
int ncol;
公众:
矩阵();
~Matrix();
作废打印();
模板
友元矩阵算子*(常数矩阵&,常数矩阵&);
};
模板
矩阵::矩阵():nrow(m),ncol(n)
{
对于(int i=0;i
./matrix.cpp:48:28: error: function template partial specialization is not allowed
    friend Matrix<T, m, l> operator*<>(const Matrix<T, m, n>&, const Matrix<T, n, l>&);
                           ^        ~~
1 error generated.
/matrix.cpp:48:28:错误:不允许函数模板部分专用化
友元矩阵算子*(常数矩阵&,常数矩阵&);
^        ~~
生成1个错误。
将其更改为:

friend Matrix<T, m, l> operator*(const Matrix<T, m, n>&, const Matrix<T, n, l>&);
友元矩阵算子*(常数矩阵&,常数矩阵&);
错误在这里:

./matrix.cpp:48:28: error: function template partial specialization is not allowed
    friend Matrix<T, m, l> operator*<>(const Matrix<T, m, n>&, const Matrix<T, n, l>&);
                           ^        ~~
1 error generated.
/matrix.cpp:48:28:错误:不允许函数模板部分专用化
友元矩阵算子*(常数矩阵&,常数矩阵&);
^        ~~
生成1个错误。
将其更改为:

friend Matrix<T, m, l> operator*(const Matrix<T, m, n>&, const Matrix<T, n, l>&);
友元矩阵算子*(常数矩阵&,常数矩阵&);

我必须更改Richard更改的内容,但我还必须更改
操作符*
朋友的声明,如下所示:

template<class T, int m, int n, int l>
Matrix<T, m, l> operator*(const Matrix<T, m, n>&, const Matrix<T, n, l>&);
          // ^ here

这似乎不太正确,但我还没有清醒到可以进行进一步调试。

我必须更改Richard更改的内容,但我还必须更改
操作符*
朋友的声明,如下所示:

template<class T, int m, int n, int l>
Matrix<T, m, l> operator*(const Matrix<T, m, n>&, const Matrix<T, n, l>&);
          // ^ here

这似乎不太正确,但我还没有清醒到可以进一步调试。

向量的向量不好:数组不应该像那样“锯齿状”。向量的向量不好:数组不应该像那样“锯齿状”。这是正确的!!我已经初始化了矩阵,每个条目都等于它的行号。例如,3x2矩阵,我已经初始化了[0,0;1,1;2,2]和2x1矩阵[0;1],将它们相乘得到[0;1;2]。非常感谢^^我有一个简短的问题。为什么我需要另一个类_T和整数_m和_n?我已经实例化了它们(类T,int m,int)当我声明一个矩阵时。再次感谢你,我希望能得到关于我问题的评论。^^简短的回答是因为编译器抱怨如果我试图使用类
T
m
,和
n
;我采取了简单的方法,给了它不同的名称,它就工作了。这可能与声明
运算符有关r*
在课堂外使用
friend
而不是只在课堂上使用
操作符*
。我能够让课堂上的
操作符*
正常工作,但它看起来确实有点奇怪。这是正确的!!我已经初始化了矩阵,每个条目都等于它的行号。例如,3x2矩阵,我已经[0,0;1,1;2,2]和2x1矩阵[0;1],将它们相乘得到[0;1;2]。非常感谢^^我有一个简短的问题。为什么我需要另一个类_T和整数_m和_n?我已经实例化了它们(类T,int m,int)当我声明一个矩阵时。再次感谢你,我希望能得到关于我问题的评论。^^简短的回答是因为编译器抱怨如果我试图使用类
T
m
,和
n
;我采取了简单的方法,给了它不同的名称,它就工作了。这可能与声明
运算符有关r*
在课外使用
朋友
而不是只在课堂上使用
操作符*
。我能够让一个课内
操作符*
正常工作,但看起来确实有点奇怪。