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

これは、クラス「sau_timer」のコンストラクターにあるコードです (別のスレッドでタイマーを実行し、別の関数を呼び出すことを願っています)。

残念ながら、atm をコンパイルしようとすると、次のエラーが発生します。

1>c:\program files\boost\boost_1_39\boost\bind\bind.hpp(246): エラー C2064: 項は 1 つの引数を取る関数に評価されません

同様に、たくさんの警告があります。私は何を間違っていますか?考えられることはすべて試しました、ありがとう。

4

3 に答える 3

4

説明は、エラー メッセージの最後にあります。

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

boost::thread の ctor の生成中にエラーが発生します。関数オブジェクト (operator()() を持つもの) を期待し、io::service とは何か (私が推測する) を渡します。io_service::run を呼び出すスレッドが必要な場合は、次のように記述します。

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

比較的最近のバージョンの Boost を使用している場合、スレッドの ctor には bind() を処理する便利なオーバーロードがあり、次のように簡単に記述できると思います。

boost::thread thrd(&io_service::run, &io);
于 2009-08-26T21:44:13.153 に答える
0

各非静的メンバー関数には、最初の隠しパラメーター (関数が呼び出されるインスタンス) があります。したがって、exec 関数には 2 つの引数が必要です。適切なコードがありますが、コメントアウトされています。つまり:

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

試してみて、他に問題がありましたか?

于 2009-08-26T19:59:43.787 に答える
0

strnd.wrap() でも使用する必要があります。これを再び次のように変更しました。

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

しかし今、私はこれらのエラーを受け取ります:

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)

========== ビルド: 0 成功、1 失敗、0 最新、0 スキップ ==========

于 2009-08-26T20:28:18.783 に答える