1
struct A
{
  A(int v):value(v){}
  int someFun(){return value;}
  int someOtherFun(int v=0){return v+value;}
  int value;
};

int main()
{
    boost::shared_ptr<A> a(new A(42));
    //boost::function<int()> b1(bind(&A::someOtherFun,a,_1)); //Error
    boost::function<int()> b2(bind(&A::someFun,a));
    b2();
    return 0;
}

bind(&A::someOtherFun,a)();编译错误失败:错误:非静态成员函数的无效使用

如何绑定类似于 someFun 的 someOtherFun?即,它们应该绑定到相同的 boost::function 类型。

4

1 回答 1

2

A::someFun()并且A::someOtherFun()有不同的类型:第一个不需要参数,第二个需要 1(可以省略,编译器会为您插入默认值)

尝试:

bind(&A::someOtherFun, a, _1)(1);

问题是,当您通过调用函数时bind(),编译器不知道该绑定函数有一个默认参数值,因此会给您错误,因为您没有所需的参数

于 2012-06-07T12:35:44.293 回答