【发布时间】:2015-10-15 16:19:19
【问题描述】:
我正在尝试将自定义转换运算符写入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);
}
我遇到了一个令人讨厌的编译时错误,这里列出的时间太长了,开头是:
错误:没有匹配的函数调用 'Eigen::Matrix::_init1(const MatrixView&)'
注意:无法将“x”(类型“const MatrixView”)转换为类型“Eigen::Index {aka long int}” 基础::模板_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 的提示!我会结束这个问题。
【问题讨论】:
-
该注释暗示让您的运营商
const。值得一试。 -
@RollenD'Souza 仍然没有区别......我实际上编辑了代码并添加了
const,所以现在很明显我没有违反const-ness。 -
@vsoftco 代码中没有任何
x,但它出现在错误消息中。 -
可能有 Homebrew 或其他版本的 eigen 干扰。尝试输出版本:
#include <Eigen\src\Core\util\Macros.h> std::cout << EIGEN_WORLD_VERSION << "." << EIGEN_MAJOR_VERSION << "." << EIGEN_MINOR_VERSION << "\n"; -
@vsoftco 当我使用开发版时,我得到了
3.2.91。确保您包含 Eigen 3.2.6 而不是一些中间版本。