以下代码导致cl.exe崩溃(MS VS2005)。
我正在尝试使用boost绑定来创建一个函数来调用myclass的方法:
#include "stdafx.h"
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <functional>
class myclass {
public:
void fun1() { printf("fun1()\n"); }
void fun2(int i) { printf("fun2(%d)\n", i); }
void testit() {
boost::function<void ()> f1( boost::bind( &myclass::fun1, this ) );
boost::function<void (int)> f2( boost::bind( &myclass::fun2, this ) ); //fails
f1();
f2(111);
}
};
int main(int argc, char* argv[]) {
myclass mc;
mc.testit();
return 0;
}
我做错了什么?
发布于 2022-01-29 11:28:10
下面的方法对我很有效。
class test_component
{
private:
void on_wait_complete(const int i);
};
void test_component::on_wait_complete (const int i)
{
cout << "on_wait_complete was called" << endl;
return;
}
int main()
{
// timerPtr_ is a variable declared in class test_component.
timerPtr_->async_wait(boost::bind(&test_component::on_wait_complete, this, _1));
}
https://stackoverflow.com/questions/2304203
复制相似问题