C++ 将boost::function与实例方法一起使用
我尝试使用下面的示例将boost::function与实例方法结合使用C++ 将boost::function与实例方法一起使用,c++,boost-function,C++,Boost Function,我尝试使用下面的示例将boost::function与实例方法结合使用 class someclass { public: int DoIt(float f, std::string s1) { return 0; } int test(boost::function<int(float, std::string)> funct) { //Funct should be pointing to DoIt m
class someclass
{
public:
int DoIt(float f, std::string s1)
{
return 0;
}
int test(boost::function<int(float, std::string)> funct)
{
//Funct should be pointing to DoIt method here
funct(12,"SomeStringToPass");
}
void caller()
{
test(DoIt); //Error : 'someclass::DoIt': function call missing argument list; use '&someclass::DoIt' to create a pointer to member
}
};
class-someclass
{
公众:
int DoIt(浮点f,标准::字符串s1)
{
返回0;
}
int测试(boost::function funct)
{
//Funct应该在这里指向DoIt方法
funct(12,“SomeStringToPass”);
}
void caller()
{
test(DoIt);//错误:“someclass::DoIt”:函数调用缺少参数列表;使用“&someclass::DoIt”创建指向成员的指针
}
};
关于如何解决此问题,您有什么建议吗?您应该使用
boost::bind
:
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>
using namespace std;
class someclass
{
public:
int DoIt(float f, std::string s1)
{
return 0;
}
int test(boost::function<int(float, std::string)> funct)
{
return funct(5.0, "hello");
}
void caller()
{
cout << test(boost::bind(&someclass::DoIt, this, _1, _2)) << endl;
}
};
int main() {
someclass s;
s.caller();
}
#包括
#包括
#包括
#包括
使用名称空间std;
上课
{
公众:
int DoIt(浮点f,标准::字符串s1)
{
返回0;
}
int测试(boost::function funct)
{
返回函数(5.0,“你好”);
}
void caller()
{
不能使用std::function
和std::bind
如果可以的话…谢谢你这么做了-标记为Timer后的答案没有问题Rajeshwar,你能将这个答案标记为正确答案吗?它是左边的勾号。谢谢。