C++错误代码C2784无法推导的模板参数

C++ Error Code C2784 could not deduce template argument for

本文关键字:参数 错误代码 C2784 C++      更新时间:2023-10-16

我对面向对象编程和C++非常陌生。我一直在研究矩阵类和平方矩阵类,遇到了一些我似乎无法解决的问题。我得到的错误代码是:

C2784:
'matrix<T,m,k> operator *(matrix<T,m,n> &,matrix<T,n,k> &)': could not 
deduce template argument for 'matrix<T,m,n> &' from 
'std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>'

我真的不确定为什么,因为我的代码还有其他部分要做。错误报告在的"product.elements=product.elements*elements;"行中

//Source.cpp
#include"Squarematrix.h"
#include<iostream>
#include<vector>
using namespace std;
int main() {
    vector<double> a = { 1, 2,4,5,6};
    squarematrix<double,2> N;
    N.assign(a);
    cout << N << N.pow(2)<< endl;
    return(0);
}
//Matrix.h
#ifndef _Matrix_
#define _Matrix_
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
template<class T, int m, int n>
class matrix {
public:
    vector<vector<T>> elements;
    int nrow;
    int ncol;
    matrix();
    matrix(matrix<T, m, n>&);
 };
template<class T, int m, int n>
matrix<T, m, n>::matrix() {
    vector<T>temp(n, 0);
    elements.assign(m, temp);
    nrow = m;  //m=0
    ncol = n;  //n=0
}
template<class T, int m, int n>
matrix<T, m, n>::matrix(matrix<T, m, n>& a) {
    elements = a.elements;
    nrow = m;
    ncol = n;
}
template<class T, int m, int n, int k>
matrix<T, m, k> operator*(const matrix<T, m, n>& a, const matrix<T, n, k>& b) {
matrix<T, m, k> product;
for (int i = 0; i < m; i++) {
    for (int j = 0; j < k; j++) {
        for (int h = 0; h < n; h++)
            product.elements[i][j] += a.elements[i][h] * b.elements[h][j];
    }
}
return product;
}
template<class T, int m, int n>
ostream& operator<< (ostream& o, const matrix<T, m, n>& input) {
    for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j)
    o << input.elements[i][j] << " ";
    o << endl;
    }
    return o;
}
#endif _Matrix_
//Squarematrix.h
#ifndef _Squarematrix_
#define _Squarematrix_
#include "Matrix.h"
#include <iostream>
#include <vector>
using namespace std;
template<class T, int n>
class squarematrix : public matrix<T, n, n> {
public:
    squarematrix();
    squarematrix(squarematrix<T, n>&);
    squarematrix<T, n> pow(int); //calculate A^k
}; 
template<class T, int n>
squarematrix<T, n>::squarematrix(){
    vector<T>temp(n, 0);
    elements.assign(n, temp);
    nrow = n;  //n=0
    ncol = n;  //n=0
}
template<class T, int n>
squarematrix<T, n>::squarematrix(squarematrix<T, n>& a){
    elements = a.elements;
    nrow = n;
    ncol = n;
}
template<class T, int n>
squarematrix<T, n> squarematrix<T, n>::pow(int k){
    squarematrix<T, n> product;
    product.elements = elements;
    for (int power = 2; power <= k; power++) {
    product.elements = product.elements * elements;  
    }
    return product;
}
#endif _Squarematrix_

您不需要nrowncol——它们是模板参数,在编译时已知。

但这不是问题——你要乘以std::vector,而你应该乘以squarematrix:

template<class T, int n>
squarematrix<T, n> squarematrix<T, n>::pow(int k){
    squarematrix<T, n> product = unit_matrix<T, n>();
    for (int power = 1; power <= k; power++) {
        product = product * *this;
    }
    return product;
}

我用了一个虚构的函数来创建一个单位矩阵
把剩下的函数写成练习。

此代码product.elements = product.elements * elements表示您希望使用两个std::vector进行乘法运算,但不支持使用两个类型为std::vector的参数进行operator *运算。在您的代码中,您支持类型矩阵的operator *操作,因此如果您想使用它,您应该将代码product.elements = product.elements * elements更改为product.elements = (product * *this).elements

那就可以了。所以类CCD_ 12的成员函数pow的代码是:

template<class T, int n>
squarematrix<T, n> squarematrix<T, n>::pow(int k){
    squarematrix<T, n> product;
    product.elements = this->elements;
    for (int power = 2; power <= k; power++) {
        product.elements = (product * *this).elements;
    }       
    return product;
}           

最后,#endif是一些预定义的宏的末尾,不要跟在某个宏后面,否则编译器会抛出一些警告或错误。