我正在使用Eigen,我正在嘗試編寫一個函數來操作矩陣的行。我遵循guidelines in the docs但我沒有嘗試編譯(使用clang或g ++);我在我的智慧結束。實際上應該如何編寫將使用RowXpr的函數?本徵,函數RowXpr
僅供參考,這裏是我到目前爲止已經試過:
#include <iostream>
#include <Eigen/Core>
using namespace std;
using namespace Eigen;
constexpr Eigen::StorageOptions Order = ColMajor;
using vect_t = Matrix<double, 1, 3, Order>;
using matr_t = Matrix<double, Dynamic, 3, Order>;
#define FUNC 3
#if FUNC == 1
vect_t func(const vect_t& f)
{
return f;
}
#elif FUNC == 2
vect_t func(const Ref<vect_t>& f)
{
return f;
}
#elif FUNC == 3
template<class D> vect_t func(const MatrixBase<D>& f)
{
return f;
}
#endif
int main()
{
matr_t M = matr_t::Random(5,3);
cout << M << endl;
cout << func(M.row(2)) << endl;
return 0;
}
謝謝!
編輯:
鏗鏘(3.8.0-2ubuntu4版)我得到的錯誤是如下。該錯誤與g ++相當。
[email protected]:~/workspace/scratch$ clang++ eigen_func_test.cpp -I /home/dan/Downloads/eigen_3.3.3/ --std=c++11 && ./a.out
In file included from eigen_func_test.cpp:2:
In file included from /home/dan/Downloads/eigen_3.3.3/Eigen/Core:436:
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/PlainObjectBase.h:899:7: error: static_assert failed
"INVALID_MATRIX_TEMPLATE_PARAMETERS"
...EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/util/StaticAssert.h:32:40: note: expanded from macro
'EIGEN_STATIC_ASSERT'
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
^ ~
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/PlainObjectBase.h:535:7: note: in instantiation of member function
'Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 1, 3> >::_check_template_params' requested here
_check_template_params();
^
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/Matrix.h:379:9: note: in instantiation of function template
specialization 'Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 1, 3>
>::PlainObjectBase<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here
: Base(other.derived())
^
eigen_func_test.cpp:32:9: note: in instantiation of function template specialization 'Eigen::Matrix<double, 1, 3, 0,
1, 3>::Matrix<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here
return f;
^
eigen_func_test.cpp:41:10: note: in instantiation of function template specialization
'func<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here
cout << func(M.row(2)) << endl;
^
1 error generated.
你得到了什麼編譯器錯誤?我所看到的唯一可能存在問題的兩件事就是你的函數模板期望返回一個'vect_t'類型,並且你返回'MatrixBase'的'const ref'。同樣在你的主函數中,另外一個問題可能是你試圖打印'matr_t'類型的'M',而不看文檔,我不知道它們是否有一個重載的輸出流操作符。 –
嗨@FrancisCugler,這與cout無關,如果我註釋掉函數調用,此行將起作用。我已經添加了一個編譯器輸出的例子。 – Dan
好吧,我現在可以看到它不是來自流操作符,而是來自模板參數本身。這似乎來自您的函數模板。我可以添加一個答案,因爲我不知道這個庫,所以我可以盡我所能去嘗試給你一些關於我認爲編譯器試圖對你的源代碼進行嘗試的知識。 –