C++ boost::bind不';不能使用指针参数

C++ boost::bind不';不能使用指针参数,c++,boost-bind,C++,Boost Bind,我有一个简单的程序。在这里,我尝试将成员函数与对象绑定,并在稍后使用成员函数调用中所需的参数进行调用。 当成员函数使用指向整数的指针时,gcc无法编译。使用integer参数编译程序。这是boost::bind的bug还是我遗漏了什么 // Doesn't work with pointer #include <iostream> #include <boost/bind.hpp> using namespace std; using namespace boost;

我有一个简单的程序。在这里,我尝试将成员函数与对象绑定,并在稍后使用成员函数调用中所需的参数进行调用。 当成员函数使用指向整数的指针时,gcc无法编译。使用integer参数编译程序。这是boost::bind的bug还是我遗漏了什么

// Doesn't work with pointer
#include <iostream>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
    float& f(int *aa) {
      cout << *aa << endl;
      return f_;
    }
};

int main() {
    X x;

    x.f_=200.1;
    int j = 10;

    cout << bind(&X::f, ref(x), _1)(&j) << endl;    
}
//不适用于指针
#包括
#包括
使用名称空间std;
使用名称空间boost;
结构X
{
浮动f_2;;
浮动和浮动(整数*aa){

cout要绑定的第一个参数应该是指向要应用非静态方法的对象的指针:

cout << bind(&X::f, &x, _1)(&j) << endl;  

cout我已经测试了问题代码是否适用于boost 1.40和gcc 4.4.3。我做了进一步的更改来测试更多的场景,它们都可以工作。下面的代码非常有效

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
  float& f(shared_ptr<int> aa) {
    cout << *aa << endl;
    return f_;
  }
};

struct XX
{
  float f_;
  float& f(int* aa) {
    cout << *aa << endl;
    return f_;
  }
};

struct XXX
{
  float f_;
  float& f(int* aa, int a) {
    cout << *aa << " " << a << endl;
    return f_;
  }
};


int main() {
  X x;
  XX xx;
  XXX xxx;
  xx.f_ = 2000.11;
  xxx.f_ = 20.11;

  shared_ptr<X> p(new X);

  x.f_=200.1;
  p->f_=20000.11;
  int i = 5;
  int j = 10;

  shared_ptr<int> sh(new int(100));
  x.f(sh);
  cout << bind(&X::f, ref(x), _1)(sh) << endl;      // x.f(i)
  cout << bind(&X::f, &x, _1)(sh) << endl;          //(&x)->f(i)
  cout << bind(&X::f, x, _1)(sh) << endl;           // (internal copy of x).f(i)
  cout << bind(&X::f, p, _1)(sh) << endl;           // (internal copy of p)->f(i)

  cout << bind(&XX::f, ref(xx), _1)(&j) << endl;        // x.f(i)
  cout << bind<float&>(mem_fn(&XXX::f), ref(xxx), _1, _2)(&j, i) << endl;       // x.f(i)
}
#包括
#包括
#包括
使用名称空间std;
使用名称空间boost;
结构X
{
浮动f_2;;
浮球和浮球(共用){

cout Pointer版本适用于g++4.2.1(osx)您在什么环境下测试此代码?您使用的是什么版本的gcc和Boost?我使用gcc 4.3和Boost 1.42测试了这两个示例,没有出现编译错误。第一个版本为我编译,尽管我做了一些小的更改,例如没有加入“使用命名空间”使用函数而不是main。指针版本可以与Boost 1.42配合使用,使用MinGW g++4.4.1和MSVC 10.0。使用哪个版本的Boost?使用哪个编译器?这相当于cout-Hmm…你是对的。我从来没有使用过那种形式。我使用了我提供的形式,它工作正常。
cout << bind(&X::f, &x, _1)(&j) << endl;  
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
  float f_;
  float& f(shared_ptr<int> aa) {
    cout << *aa << endl;
    return f_;
  }
};

struct XX
{
  float f_;
  float& f(int* aa) {
    cout << *aa << endl;
    return f_;
  }
};

struct XXX
{
  float f_;
  float& f(int* aa, int a) {
    cout << *aa << " " << a << endl;
    return f_;
  }
};


int main() {
  X x;
  XX xx;
  XXX xxx;
  xx.f_ = 2000.11;
  xxx.f_ = 20.11;

  shared_ptr<X> p(new X);

  x.f_=200.1;
  p->f_=20000.11;
  int i = 5;
  int j = 10;

  shared_ptr<int> sh(new int(100));
  x.f(sh);
  cout << bind(&X::f, ref(x), _1)(sh) << endl;      // x.f(i)
  cout << bind(&X::f, &x, _1)(sh) << endl;          //(&x)->f(i)
  cout << bind(&X::f, x, _1)(sh) << endl;           // (internal copy of x).f(i)
  cout << bind(&X::f, p, _1)(sh) << endl;           // (internal copy of p)->f(i)

  cout << bind(&XX::f, ref(xx), _1)(&j) << endl;        // x.f(i)
  cout << bind<float&>(mem_fn(&XXX::f), ref(xxx), _1, _2)(&j, i) << endl;       // x.f(i)
}