首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

boost:bind失败解决方案

2013-09-09 
boost::bind失败class Point{public:Point(int a 0, int b 0):x(a),y(b){}inline void Print(int val

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 );

热点排行