views:

1479

answers:

3
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).

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

Aswell as a whole bunch of warnings. What am I doing wrong? I've tried everything I can think of, thank you.

A: 

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. 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?

Tadeusz Kopec
See my answer..
Motig
A: 

I need it to be used with strnd.wrap() aswell. 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 ==========

Motig
+2  A: 

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. It expects a function object (something with an opererator()()), and you pass it what (I guess) is an io::service. If what you want is a thread calling io_service::run, write:

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::thread thrd(&io_service::run, &io);
Éric Malenfant