boost::bind 的问题
以下代码在我想要创建 Test::fun2
函子对象的行抛出错误:
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
using namespace boost;
class Test
{
public:
float fun1() { return 0.0f; }
void fun2( float x ) {}
};
int main( int argc, char* argv[] )
{
shared_ptr<Test> p = shared_ptr<Test>( new Test );
function<float(void)> f1 = bind( &Test::fun1, p );
function<void(float)> f2 = bind( &Test::fun2, p );
return 1;
}
编译器给了我一组模板错误,
`void (Test::*)(float)' is not a class, struct, or union type
这似乎是主要错误。尽管如此,我不知道这里有什么问题以及如何解决它。
The following code throws an error at the line where I want to create a functor object of Test::fun2
:
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
using namespace boost;
class Test
{
public:
float fun1() { return 0.0f; }
void fun2( float x ) {}
};
int main( int argc, char* argv[] )
{
shared_ptr<Test> p = shared_ptr<Test>( new Test );
function<float(void)> f1 = bind( &Test::fun1, p );
function<void(float)> f2 = bind( &Test::fun2, p );
return 1;
}
The compiler gives me a set of template errors and
`void (Test::*)(float)' is not a class, struct, or union type
which seems to be the main error. Nevertheless I have no idea what's the problem here and how to solve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题已解决:我使用了错误的语法。
函数 f2 = 绑定( &Test::fun2, p, _1 );
作品。
Problem solved: I used the wrong syntax.
function f2 = bind( &Test::fun2, p, _1 );
works.