c++ - Eigen MatrixBase模板函数

标签 c++ compiler-errors eigen

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

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 个错误。

最佳答案

您的声明要求 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) { ... }

关于c++ - Eigen MatrixBase模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18160043/

相关文章:

python - 将 Python 函数传递给 Boost C++

c++ - 在 C++ 中将输出写入文件的问题

c - 在 Solaris 上构建 Node.JS : "Use of <stdbool.h> is valid only in a c99 compilation environment."

c++ - 从 Eigen 中的矩阵中提取 vector 的正确方法是什么?

c++ - 看似正确的模板代码出错 - 怎么了?

c++ - 数组的正确值会提高性能吗?

c++ - 如何在 Google Mock 中匹配参数引用

android - 运行非零退出值1时出现此错误

compiler-errors - 在Debian 8.6上安装CouchDB 2.0.0失败

c++ - 是否有像 boosts bcp for Eigen 这样的工具?