【问题标题】:How do I apply the .cast() method to the output of a comparison operator?如何将 .cast() 方法应用于比较运算符的输出?
【发布时间】:2014-05-04 02:07:12
【问题描述】:

我想将阈值函数应用于一堆模糊值(浮点数):

template <typename Derived>
void Threshold(Eigen::ArrayBase<Derived>& params)
{
  params = (params >= 0.0f).cast<float>();
}

我的想法是我最终得到一堆 1 和 0 (float),其中曾经有模糊值。但是当我这样做时,我收到以下错误:

src/core/Neuron.h:在静态成员函数'static void 神经元::阈值(Eigen::ArrayBase&)': src/core/Neuron.h:119:43:错误:预期的主表达式之前 ‘float’ 参数 = (params >= 0.0f).cast(); ^ src/core/Neuron.h:119:43: 错误:在“float”之前预期的“;” make: * [src/core/Cell.o] 错误 1

显然我没有正确使用cast 方法。我查看了Documentation,它为cast 方法提供了以下原型:

internal::cast_return_type< Derived,const CwiseUnaryOp<internal::scalar_cast_op<typename internal::traits<Derived>::Scalar, NewType>, const Derived> >::type cast () const inline

我不太明白这意味着什么,但我猜我可能缺少模板参数?

【问题讨论】:

    标签: c++ eigen


    【解决方案1】:

    试试:

    params = (params >= 0.0f).template cast<float>();
    

    这种用法基本上只是template 关键字的重载。在这里,它是对编译器的提示,您正在尝试调用成员模板。详情请参阅this

    【讨论】:

    • 那行得通。你能解释一下为什么吗?我真的不明白你使用的语法..
    • 我添加了一个简短的解释,以及一个(长得多)的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2018-05-27
    • 1970-01-01
    • 2020-03-14
    • 2014-06-14
    • 1970-01-01
    相关资源
    最近更新 更多