2009-08-26 25 views
1
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); 
} 

Dies ist der Code, den ich im Konstruktor für die Klasse 'sau_timer' habe (die hoffentlich einen Timer in einem separaten Thread laufen lassen wird und dann rufe eine andere Funktion auf).boost :: bind, boost :: asio, boost :: thread und classes

Leider atm, wenn ich zu kompilieren versuchen, erhalte ich folgende Fehlermeldung:

1> c: \ program files \ boost \ boost_1_39 \ boost \ bind \ bind.hpp (246): Fehler C2064: term bewertet nicht zu einer Funktion, die 1 Argumente nimmt

Sowie eine ganze Reihe von Warnungen. Was mache ich falsch? Ich habe alles versucht, was mir einfällt, danke.

+0

Konnten Sie sau_timer :: exec Erklärung geben? Ist es eine no-arg-Member-Funktion, eine arg statische Funktion oder etwas anderes? –

+0

void sau_timer :: exec (const boost :: system :: fehlercode &) { \t (f) (params); } – Xenoprimate

Antwort

4

die Erklärung am Ende der Fehlermeldungen ist:

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 

der Fehler tritt auf, während die Erzeugung von ctor boost :: thread. Es erwartet ein Funktionsobjekt (etwas mit einem operator()()), und Sie übergeben es, was (ich denke) ein io :: service ist. Wenn das, was wollen Sie ist ein Thread ruft io_service :: run, schreiben:

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

Wenn Sie eine relativ neue Version von Boost-verwenden, glaube ich, dass Threads Ctor eine Bequemlichkeit Überlastung hat, kümmert sich um die bind nimmt(), erlauben, einfach zu schreiben:

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

Jede nicht statische Elementfunktion hat einen ersten versteckten Parameter - die Instanz, für die die Funktion aufgerufen werden soll. Ihre Exec-Funktion benötigt also zwei Argumente. Und Sie haben entsprechenden Code, aber es ist auskommentiert. Ich meine:

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

Haben Sie es versucht und hatte andere Probleme?

+0

Siehe meine Antwort .. – Xenoprimate

0

Ich brauche es auch mit strnd.wrap() verwendet werden. Ich habe es auf diese wieder geändert:

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); } 

Aber jetzt bekomme ich diese Fehler:

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) 

========== Körperbau: 0 gelang, 1 schlug fehl, 0 bis -zu-Datum, übersprungen 0 ==========