function表达式不会导致接受1个参数的函数

std::function expression does not result in a function which accepts 1 Arguement

本文关键字:1个 参数 函数 表达式 function      更新时间:2023-10-16

我正在学习使用std::function来传递函数。我从MSDN网站上获取了代码代码,该代码向我展示了如何了解进程是否启动。

http://pastebin.com/gVkXZC9b

我知道void signal_onProcessStart函数是不必要的,我稍后会扩大它。

我的问题是我的编译器告诉我std::函数有一个错误。

表达式不能导致接受1个参数的函数。

我使用的是德语版本的Visual Studio,所以我进行了翻译。我认为这个错误在英语中的叫法不同。

所有需要触发的函数都有void作为返回值,HRESULT*作为唯一的参数。

的问候编辑:

我做了一个同样错误的小例子:

#include<functional>
#include<iostream>
#include<Windows.h>
using namespace std;
class workingClass
{
private:
    std::function<void(int)> f;
public:
    workingClass(std::function<void(int)> p)
    {
        this->f = p;
    }
    void triggerme(int x)
    {
        this->f(x);
    }
    ~workingClass(){}
};
class managingClass
{
private:
    function<void(int)> f;
    workingClass * ptr;
    int x;
protected:
    void trigger(int x)
    {
        this->f(x);
    }
public:
    managingClass(function<void(int)> f)
    {
        this->f = f;
        function<void(int)> tmp = bind(&managingClass::trigger, this->x);
        ptr = new workingClass(tmp);
        ptr->triggerme(20);
    }
    ~managingClass(){}
};
void triggered(int x)
{
    cout << "it is triggered";
}
int main()
{
    function<void(int)> t = bind(&triggered, 20);
    managingClass temp(t);
    system("pause");
}

错误行1149在文件功能表达式不会产生接受1

的函数。

您对bind的使用不正确。不使用任何占位符(_1和类似的)的绑定表达式会导致没有参数的函数。如果有void foo(int),则bind(&foo, 20)类型对应std::function<void()>int参数绑定为20,不能再从外部设置。

这就是bind的作用——绑定一些参数,而有选择地不绑定其他参数。例如,这将打印105:

int foo(int x, int y, int z)
{ return x * y + z; }
int main()
{
  using namespace std::placeholders;
  std::function<int(int, int)> b = std::bind(&foo, _1, 10, _2);
  std::cout << b(10, 5) << 'n';
}

此外,非静态成员函数在绑定时将对象(或指向对象的指针)作为其第一个实参。例如,你可以这样做:

struct Foo
{
  int id;
  int bar(int x) const
  { return 2 * x + id; }
};
int main()
{
  using namespace std::placeholders;
  Foo foo1 = {1};
  Foo foo2 = {2};
  std::function<int(int)> f1 = std::bind(&Foo::bar, foo1, _1);
  std::cout << f1(10) << 'n';  // prints 21
  std::cout << f1(20) << 'n';  // prints 41
  std::function<int(const Foo&)> f2 = std::bind(&Foo::bar, _1, 10);
  std::cout << f2(foo1) << 'n';  // prints 21;
  std::cout << f2(foo2) << 'n';  // prints 22;
  std::function<int()> f3 = std::bind(&Foo::bar, foo1, 40);
  std::cout << f3() << 'n';  // prints 81;
}

我很难说出你的代码到底想要达到什么目的,但这些信息应该足以为你指明正确的方向。

你的bind语句是错误的。

由于triggermanagingClass的成员函数,所以不能直接存储在std::function<void(int)>中。需要告诉它应该在类的哪个具体实例(即哪个对象)上调用函数。

换句话说,您可以将其视为具有void(managingClass*,int):

签名的函数。
managingClass(function<void(int)> f)
{
    std::function<void(managingClass*,int)> foo =
        std::mem_fn(&managingClass::trigger);
    // don't worry about the std::mem_fn here too much. It's required
    // to keep the compiler happy, since in C++ member function pointers
    // require different handling than ordinary function pointers,
    // but has no deeper meaning
    foo(this, 42);
}

现在要做的是去掉managingClass*参数(具体对象),但是留下第二个参数 (int),这样用户可以在调用函数时决定它的值。这就是std::placeholders的作用:

managingClass(function<void(int)> f)
{
    std::function<void(int)> foo =
        std::bind(&managingClass::trigger, this, std::placeholders::_1);
    // the bind is smart enough that we don't need the std::mem_fn in here
    foo(42);
    // note how we no longer need to pass the 'this' here
}

您应该为您的目的使用占位符。

using std::placeholders::_1;
class workingClass
{
private:
    std::function<void(int)> f;
public:
    workingClass(std::function<void(int)> p)
    {
        this->f = p;
    }
    void triggerme(int x)
    {
        this->f(x);
    }
    ~workingClass(){}
};
class managingClass
{
private:
    function<void(int)> f;
    workingClass * ptr;
    int x;
protected:
    void trigger(int x)
    {
        this->f(x);
    }
public:
    managingClass(function<void(int)> f)
    {
        this->f = f;
        function<void(int)> tmp = bind(&managingClass::trigger, this, _1);//, this->x);
                                                                //    ^^
        ptr = new workingClass(tmp);
        ptr->triggerme(20);
    }
    ~managingClass(){}
};
void triggered(int x)
{
    cout << "it is triggered";
}
int main()
{
    function<void(int)> t = bind(&triggered, _1);//, 20);
                                        //   ^^
    managingClass temp(t);
}