我正在尝试将自定义转换运算符写入 Eigen::Matrix2d
对象,但我惨败了。下面是精简的代码:
#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 Yosemite、Eigen 3.2.6、g++ (MacPorts gcc5 5.2.0_0) 5.2.0、Apple LLVM 版本 7.0.0 (clang-700.0.72) 目标:x86_64-apple-darwin14.5.0,均失败编译代码。
编辑2
整个问题实际上发生是因为我的链接指向 Eigen_3.3_alpha1 的开发人员版本。在 Eigen 3.2.x 中它可以工作。感谢@Matt 的提示!我会结束这个问题。
最佳答案
Eigen 具有定期生成的开发包。按照评论中 Drop 的建议显示如何检查正在包含哪个版本。有一个 alpha 版本于 9 月 4 日发布,返回版本 3.2.91
。使用 10 月 1 日发布的 3.2.6
版本可以正确编译。代码显示的是版本是:
#include <Eigen\src\Core\util\Macros.h>
#include <iostream>
...
std::cout << EIGEN_WORLD_VERSION << "." << EIGEN_MAJOR_VERSION << "." <<
EIGEN_MINOR_VERSION << "\n";
关于c++ - Eigen 中的转换运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153623/