0

我不知道如何修复以下代码:

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& L, const MatrixBase& b, ^ 1 产生错误。

4

1 回答 1

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) { ... }
于 2013-08-10T09:08:04.630 回答