您的函数签名不完整,但我想您缺少的主要内容是使用 template keyword for the function call :
mat2 = coefficient * mat1.template cast <typename Derived2::Scalar> ();
一个完整的工作示例:
#include <eigen3/Eigen/Core>
#include <iostream>
template<typename Derived1, typename Derived2>
void mul(const typename Derived1::Scalar& coefficient,
const Eigen::MatrixBase<Derived1>& mat1,
Eigen::MatrixBase<Derived2>& mat2)
{
mat2 = coefficient * mat1.template cast <typename Derived2::Scalar> ();
}
int main() {
Eigen::Matrix3f a;
a << 1.0, 0.0, 0.0,
0.0, 2.0, 0.0,
0.0, 0.0, 3.0;
Eigen::Matrix3i b;
b << 1, 0, 0,
0, 1, 0,
0, 0, 1;
mul(3.5, a, b);
std::cout << b << "\n";
return 0;
}
编译和运行时会打印出来
3 0 0
0 6 0
0 0 9
到标准输出。