2013-08-10 4 views
0

Я не знаю, как исправить следующий код:Эйген MatrixBase шаблон функции

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 

недействительными forw_sub (Const MatrixBase & L, Const MatrixBase & б, генерируется ^ 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) { ... } 
Смежные вопросы