简体   繁体   English

函子编译时错误

[英]Functor compile time error

My code - 我的代码-

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

#include "boost\numeric\ublas\matrix.hpp"

typedef boost::numeric::ublas::matrix<float> matrix;


class FillMat{  
public:
    FillMat(float valIn) : val(valIn){}
    float operator()(float in) {
        val = in + 1;
        return val;
    }
private:
    float val;
};

typedef boost::numeric::ublas::matrix<float> matrix;
int main(){
    matrix m1(10, 20);
    float init = 22.2;
    FillMat myFiller(init);

    generate(m1.begin2(), m1.begin2() + m1.size1()*m1.size2(), myFiller);


    return 0;
}

When I try to compile the code, I get the following compile time error. 当我尝试编译代码时,出现以下编译时错误。

Error   3   error C2064: term does not evaluate to a function taking 0 arguments    

Can someone please tell me why? 有人可以告诉我为什么吗?

PS I added the headers. PS我添加了标题。 I am using Boost matrix for 2D array. 我将Boost矩阵用于2D阵列。

The signature of the functor you pass to std::generate must take zero arguments. 传递给std::generate的函子的签名必须采用零参数。

This is stated in documentation . 这在文档中有说明

Unfortunately, you didn't tell us what you are trying to do, so I can't suggest a fix for you. 不幸的是,您没有告诉我们您要做什么,所以我不能为您提供修复建议。

A possible fix would be to change: 可能的解决方法是更改​​:

  float operator()(float in) {
        val = in + 1;
        return val;
    }

into: 变成:

   float operator()( void ) {
        float rv = val;
        val = val + 1.0;
        return rv;
    }

If that does what you want , that is the question ... 如果那符合您的要求 ,那就是问题所在...

The function you're looking for is std::transform : 您要寻找的功能是std::transform

std::transform(m1.begin2(),
               m1.begin2() + m1.size1() * m1.size2(),
               m1.begin2(),
               myFiller);

Based on the clarification in the comment ("I am trying to fill the contents of my container such with incrementing values") what you really want is std::iota . 基于注释中的澄清(“我正在尝试使用递增值填充容器的内容”),您真正想要的是std::iota I haven't quite gotten straight what you're trying to do with your matrix , and how you want the values to increment (row-wise or column-wise), so I'll give a demo with a normal vector, and let you figure out how to apply it in your actual situation: 我还没有弄清楚您要如何处理matrix ,以及如何让值递增(行或列),所以我将给出一个具有法向矢量的演示,您了解了如何在实际情况下应用它:

std::vector<int> v(10);

std::iota(v.begin(), v.end(), 22.2);

for (auto i : v)
    std::cout << i << " ";

Should produce: 22.2 23.2 24.2 25.2 26.2 27.2 28.2 29.2 30.2 31.2 应该产生: 22.2 23.2 24.2 25.2 26.2 27.2 28.2 29.2 30.2 31.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM