使用 C++ 模板组合不同大小的矩阵

问题描述 投票:0回答:1

我有一个名为 Matrix 的模板化类,它具有以下模板参数:

template <typename T, int rows, int columns> class Matrix { ... }

我想编写一个名为 Augment 的函数,它将两个不同大小的矩阵扩充(只是将两个并排组合)成一个新矩阵。例如:3x1 + 3x2 = 3x3。

用 C++ 编写的对象将如下所示:

Matrix<int, 3, 1> a; Matrix<int, 3, 2> b; Matrix<int, 3, 3> c = a.Augment(b);

我的类函数声明如下:

template <int newColumns, int otherColumns> Matrix<T, rows, newColumns> Augment(const Matrix<T, rows, otherColumns>& other)

但是,我得到一个错误:“没有函数模板的实例

"Matrix<T, rows, columns::Augment [with T=int, rows=3, columns=1]" matches the argument list argument types are: (Matrix<int, 3, 2>) object type is: Matrix<int, 3, 1>".

到目前为止,我已经尝试过这些模板参数但没有成功,我仍然遇到同样的错误:

  • 模板
  • 模板
c++ class templates
1个回答
0
投票

编译器无法使用您提供的签名推断出

newColumns
。这个怎么样:

template <int otherColumns> 
Matrix<T, rows, columns+otherColumns> Augment(const Matrix<T, rows, otherColumns>& other);
© www.soinside.com 2019 - 2023. All rights reserved.