读书人

boost:bind失败解决方案

发布时间: 2013-09-09 20:31:09 作者: rapoo

boost::bind失败

class Point
{
public:
Point(int a = 0, int b = 0):x(a),y(b){}
inline void Print(int val ) const
{
cout<<" "<<x<<" "<<y<<endl;
}
private:
int x;
int y;
};


int main()
{
vector<Point>v(10);
bind(&Point::Print,_1,_2)(5556); //为什么不能编译?
return 0;
}

按照boost bind的用法,有时成员函数指针,那么 _1是必须的, 由于成员函数有参数,所以
还需要一个占位符,所以有了_2 ,由于我要传参数,所以有了(5556)

结果错误
[解决办法]

引用:
Quote: 引用:

bind( mem_fn(&Point::Print),_1,_2)(5556);


for_each(v.begin(),v.end(),bind(&Point::Print,_1,2));

为什么这个可以?

你看,这个不需要mem_fun

少传了个参数
bind( &Point::Print , _1, _2 )( (Point*)0 , 5556 );

读书人网 >C++

热点推荐