我不知道如何修复以下代码:
template <class Derived, class OtherDerived>
void forw_sub(const MatrixBase<Derived>& L, const MatrixBase<Derived>& b,
MatrixBase<OtherDerived> const & x) {
typedef typename Derived::Scalar Scalar;
MatrixBase<OtherDerived>& x_ = const_cast< MatrixBase<OtherDerived>& >(x);
for(int i=0; i < L.rows(); i++) {
Scalar s = b(i);
for(int j=0; j < i; j++) {
s -= L(i,j)*x_(j);
}
x_(i) = s/L(i,i);
}
}
召唤时:
MatrixXd L = Matrix3d::Identity();
VectorXd b = Vector3d::Ones();
VectorXd x = Vector3d::Zero();
forw_sub(L,b,x);
生成错误:
/home/vision/workspace/sci-comp/test/test_leq.cpp: In member function ‘virtual void LEQ_FORW_SUB_Test::TestBody()’:
/home/vision/workspace/sci-comp/test/test_leq.cpp:15:16: error: no matching function for call to ‘forw_sub(Eigen::MatrixXd&, Eigen::VectorXd&, Eigen::VectorXd&)’
/home/vision/workspace/sci-comp/test/test_leq.cpp:15:16: note: candidate is:
/home/vision/workspace/sci-comp/src/leq/leq.hpp:5:6: note: template<class Derived, class OtherDerived> void forw_sub(const Eigen::MatrixBase<T>&, const Eigen::MatrixBase<T>&, const Eigen::MatrixBase<U>&)
使用clang进行编译会生成错误:
/home/vision/workspace/sci-comp/test/test_leq.cpp:15:2: error: no matching function for call to 'forw_sub'
forw_sub(L,b,x);
^~~~~~~~
/home/vision/workspace/sci-comp/src/leq/leq.hpp:5:6: note: candidate template ignored: failed template argument
deduction
void forw_sub(const MatrixBase&amp; L,const MatrixBase&amp; b, ^ 生成1个错误。
答案 0 :(得分:1)
您的声明要求L和b属于同一类型,而在您的情况下,一个是MatrixXd,另一个是VectorXd。建议的解决方案:
template <class DerivedL, class DerivedB, class DerivedX>
void forw_sub(const MatrixBase<DerivedL>& L, const MatrixBase<DerivedB>& b,
MatrixBase<OtherDerivedX> const & x) { ... }