简体   繁体   English

boost :: bind,boost :: asio,boost :: thread和classes

[英]boost::bind, boost::asio, boost::thread, and classes

sau_timer::sau_timer(int secs, timerparam f) : strnd(io), 
    t(io, boost::posix_time::seconds(secs))
{
    assert(secs > 0);
    this->f = f;

    //t.async_wait(boost::bind(&sau_timer::exec, this, _1));
    t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this)));
    boost::thread thrd(&io,this);
    io.run();
    //thrd(&sau_timer::start_timer);
}

This is the code I have in the constructor for the class 'sau_timer' (which will hopefully run a timer in a seperate thread and then call another function). 这是我在类'sau_timer'的构造函数中的代码(希望在单独的线程中运行一个计时器,然后调用另一个函数)。

Unfortunately, atm when I try to compile, I get the following error: 不幸的是,当我尝试编译时,我收到以下错误:

1>c:\\program files\\boost\\boost_1_39\\boost\\bind\\bind.hpp(246) : error C2064: term does not evaluate to a function taking 1 arguments 1> c:\\ program files \\ boost \\ boost_1_39 \\ boost \\ bind \\ bind.hpp(246):错误C2064:term不计算为带1个参数的函数

Aswell as a whole bunch of warnings. 还有一大堆警告。 What am I doing wrong? 我究竟做错了什么? I've tried everything I can think of, thank you. 我已经尝试了我能想到的一切,谢谢。

The explanation is at the end of the error messages: 解释是在错误消息的末尾:

c:\users\ben\documents\visual studio 2008\projects\sauria\sauria\sau_timer.cpp(11) :
  see reference to function template instantiation 
  'boost::thread::thread<boost::asio::io_service*,sau_timer*>(F,A1)' being compiled

The error occurs while generating the ctor of boost::thread. 生成boost :: thread的ctor时发生错误。 It expects a function object (something with an opererator()()), and you pass it what (I guess) is an io::service. 它需要一个函数对象(带有opererator()()的东西),你传递它(我猜)是一个io :: service。 If what you want is a thread calling io_service::run, write: 如果你想要的是一个调用io_service :: run的线程,请写:

boost::thread thrd(boost::bind(&io_service::run, &io));

If you use a relatively recent version of Boost, I believe that thread's ctor has a convenience overload that takes care of the bind(), allowing to simply write: 如果你使用相对较新版本的Boost,我相信线程的ctor有一个方便的重载来处理bind(),允许简单地写:

boost::thread thrd(&io_service::run, &io);

Each non-static member function has a first, hidden parameter - the instance on which function is to be called. 每个非静态成员函数都有一个第一个隐藏参数 - 要调用函数的实例。 So your exec function needs two arguments. 所以你的exec函数需要两个参数。 And you have appropriate code, but it is commented out. 你有适当的代码,但它被注释掉了。 I mean: 我的意思是:

t.async_wait(boost::bind(&sau_timer::exec, this, _1));

Did you try it and had some other problems? 你尝试过它还有其他一些问题吗?

I need it to be used with strnd.wrap() aswell. 我需要它与strnd.wrap()一起使用。 I've changed it to this again: 我又把它改成了这个:

sau_timer::sau_timer(int secs, timerparam f) : strnd(io), 
    t(io, boost::posix_time::seconds(secs))
{
    assert(secs > 0);
    this->f = f;

    t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this, _1)));
    boost::thread thrd(&io);
    io.run();
}
void sau_timer::exec(const boost::system::error_code&) { (f)(params); }

But now I get these errors: 但现在我得到了这些错误:

1>------ Build started: Project: Sauria, Configuration: Debug Win32 ------
1>Compiling...
1>sau_timer.cpp
1>Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1>- add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1>- add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1>Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1>c:\program files\boost\boost_1_39\boost\bind\bind.hpp(246) : error C2064: term does not evaluate to a function taking 1 arguments
1>        c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(20) : see reference to function template instantiation 'void boost::_bi::list1<A1>::operator ()<F,boost::_bi::list0>(boost::_bi::type<T>,F &,A &,int)' being compiled
1>        with
1>        [
1>            A1=boost::_bi::value<sau_timer *>,
1>            F=boost::asio::io_service *,
1>            T=void,
1>            A=boost::_bi::list0
1>        ]
1>        c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(18) : while compiling class template member function 'void boost::_bi::bind_t<R,F,L>::operator ()(void)'
1>        with
1>        [
1>            R=void,
1>            F=boost::asio::io_service *,
1>            L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
1>        ]
1>        c:\program files\boost\boost_1_39\boost\thread\detail\thread.hpp(227) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1>        with
1>        [
1>            R=void,
1>            F=boost::asio::io_service *,
1>            L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
1>        ]
1>        c:\users\ben\documents\visual studio 2008\projects\sauria\sauria\sau_timer.cpp(11) : see reference to function template instantiation 'boost::thread::thread<boost::asio::io_service*,sau_timer*>(F,A1)' being compiled
1>        with
1>        [
1>            F=boost::asio::io_service *,
1>            A1=sau_timer *
1>        ]
1>Build log was saved at "file://c:\Users\Ben\Documents\Visual Studio 2008\Projects\Sauria\Sauria\Debug\BuildLog.htm"
1>Sauria - 1 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== ==========构建:0成功,1失败,0最新,0跳过==========

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM