【问题标题】:Conversion operator in EigenEigen 中的转换运算符
【发布时间】: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 &lt;Eigen\src\Core\util\Macros.h&gt; std::cout &lt;&lt; EIGEN_WORLD_VERSION &lt;&lt; "." &lt;&lt; EIGEN_MAJOR_VERSION &lt;&lt; "." &lt;&lt; EIGEN_MINOR_VERSION &lt;&lt; "\n";
  • @vsoftco 当我使用开发版时,我得到了3.2.91。确保您包含 Eigen 3.2.6 而不是一些中间版本。

标签: c++ eigen eigen3


【解决方案1】:

Eigen 具有定期生成的开发包。按照 Drop in the cmets 的建议,展示了如何检查包含的版本。 9 月 4 日发布了一个 alpha 版本,返回版本 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";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    相关资源
    最近更新 更多