我需要将一个参数绑定到类成员函数。 事情是这样的:std ::将参数绑定到没有对象的成员函数
#include <functional>
#include <iostream>
struct test
{
void func(int a, int b)
{
std::cout << a << " " << b << std::endl;
}
};
int main(int argc, char** argv)
{
typedef void (test::*TFunc)(int);
TFunc func = std::bind(&test::func, 1, std::placeholders::_1);
}
但在这种情况下,我有编译错误
error: static assertion failed: Wrong number of arguments for pointer-to
-member
您可能不应该期望'std :: bind'生成的对象可以转换为普通成员函数指针... – Quentin
如果您正在寻找一种基本上在类定义之外定义成员函数的方法,那么这是无法完成的。您只能向该类添加重载或定义一个自由函数。 –