function表达式不会导致接受1个参数的函数
std::function expression does not result in a function which accepts 1 Arguement
我正在学习使用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语句是错误的。
由于trigger
是managingClass
的成员函数,所以不能直接存储在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);
}
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 为什么 c++11 std::lock 和 std::scoped_lock 至少需要 2 个参数?
- 具有多个参数的模板化类专用化,其中一个模板参数是模板本身
- 在 masm x64 上打印具有多个参数
- C++ 具有 2 个模板参数的类,具有 1 个参数的函数
- 可变参数模板作为第一个参数
- 在C++中使用 std::forward 的多个参数
- 如果条件,当我想第二个参数时
- 如何在 c++ 中存储具有值的四个参数的元组
- 将多个参数传递给运算符 []
- C++ 具有GTest TYPED_TEST的多个参数
- 如何从第一个参数推断第二个参数类型?
- C++ std::enable_if - 第二个参数的功能
- std::bind 和 std::函数术语不值为接受 0 个参数?
- 推导多个参数包
- 候选构造函数(隐式复制构造函数)不可行:第一个参数需要 l 值
- 为什么模板参数推导不适用于仅指定前两个参数的可变参数模板类?
- C++如何在 switch 语句中放置两个参数
- 生成包含给定类型的 N 个参数的可变参数列表的最佳方法?