2015-10-15 71 views
2

我正在嘗試將自定義轉換運算符寫入Eigen::Matrix2d對象,並且我慘敗了。下面是剝離到的骨代碼:Eigen中的轉換運算符

#include <iostream> 
#include <Eigen/Dense> 

struct MatrixView 
{ 
    operator Eigen::Matrix2d() const // conversion operator 
    { 
     Eigen::Matrix2d tmp; 
     std::cout << "CONVERSION TRIGERRED\n"; 
     return tmp; 
    } 
}; 

int main() 
{ 
    MatrixView m; 
    static_cast<Eigen::Matrix2d>(m); 
} 

我得到一個討厭的編譯時錯誤,太長的方式在這裏列出,首先是:

error: no matching function for call to 'Eigen::Matrix::_init1(const MatrixView&)'

note: cannot convert 'x' (type 'const MatrixView') to type 'Eigen::Index {aka long int}' Base::template _init1(x); Base::template _init1(x);

您可以找到完整的錯誤訊息here

我不知道發生了什麼事情,轉換運算符是微不足道的,它只是返回默認初始化的Eigen::Matrix2d。任何想法有什麼問題嗎?

編輯

如果我刪除 「明確」 則轉換是通過複製初始化觸發,像

Eigen::Matrix2d tmp = m; // OK without "explicit" 

然而static_cast仍然失敗。

平臺細節:

OS X 10.10約塞米蒂,本徵3.2.6,克++(MacPorts的gcc5 5.2.0_0)5.2.0,蘋果LLVM版本7.0.0(鐺-700.0.72)靶: x86_64-apple-darwin14.5.0,都無法編譯代碼。

EDIT 2

整個事件實際發生的,因爲我的鏈接是指向Eigen_3.3_alpha1的開發者版本。在Eigen 3.2.x中它可以工作。感謝@Matt的提示!我會結束這個問題。

+0

該注意事項使您的操作符爲'const'。值得一試。 –

+0

@ RollenD'Souza仍然沒有區別......我實際編輯了代碼並添加了'const',所以現在很清楚我沒有違反'const''。 – vsoftco

+0

不是'Matrix2d'只是'Matrix 的模板。你有沒有嘗試過使用底層的'Matrix'本身? – Matt

回答

2

Eigen有定期生產的開發包。遵循評論中的Drop的建議顯示如何檢查包含哪個版本。在9月4日發佈的alpha中有一個返回版本3.2.91。使用版本3.2.6在10月1日發佈的編譯正確。代碼顯示的版本是:

#include <Eigen\src\Core\util\Macros.h> 
#include <iostream> 
... 
std::cout << EIGEN_WORLD_VERSION << "." << EIGEN_MAJOR_VERSION << "." << 
    EIGEN_MINOR_VERSION << "\n";