2015-10-15 102 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";