2011-06-28 23 views
1

私はこのエラーを取得する理由を私は理解することはできません。ブースト:: ASIO :: async_readバインドコンパイルエラー

/usr/local/include/boost/asio/impl/read.hpp: In member function ‘void boost::asio::detail::read_op<AsyncReadStream, boost::asio::mutable_buffers_1, CompletionCondition, ReadHandler>::operator()(const boost::system::error_code&, size_t, int) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, CompletionCondition = boost::asio::detail::transfer_at_least_t, ReadHandler = boost::function<void()(long unsigned int)>]’: 
/usr/local/include/boost/asio/impl/read.hpp:263: instantiated from ‘void boost::asio::async_read(AsyncReadStream&, const MutableBufferSequence&, CompletionCondition, ReadHandler) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, MutableBufferSequence = boost::asio::mutable_buffers_1, CompletionCondition = boost::asio::detail::transfer_at_least_t, ReadHandler = boost::function<void()(long unsigned int)>]’ 
src/communicator/protocol/Command.cc:34: instantiated from here 
/usr/local/include/boost/asio/impl/read.hpp:215: error: no match for call to ‘(boost::function<void()(long unsigned int)>) (const boost::system::error_code&, const long unsigned int&)’ 
/usr/local/include/boost/function/function_template.hpp:1007: note: candidates are: typename boost::function1<R, T1>::result_type boost::function1<R, T1>::operator()(T0) const [with R = void, T0 = long unsigned int] 
make: *** [src/communicator/protocol/Command.o] Error 1 

ここに私のクラス: Command.hh

namespace communicator {            
    namespace protocol {             
    namespace in {              
     class Command : public boost::enable_shared_from_this<Command> { 
     public:               
     ~Command();             

     typedef boost::shared_ptr<Command>  pointer;    

     void got_newline();           

     protected:              
     Command(tcp::socket& socket, structure::Client& client) :  
      m_socket(socket), m_client(client) {}; 

     void endParsing(); 

     tcp::socket&   m_socket;        

     structure::Client&  m_client;        
     char     m_newline[2];       
     private:        

     };                
    } 
} 

Command.cc :

namespace communicator { 
    namespace protocol { 
    namespace in { 

     void Command::endParsing() { 
     boost::function<void()> cb = boost::bind(&Command::got_newline, 
              shared_from_this()); 
     boost::asio::async_read(m_socket, 
           boost::asio::buffer(m_newline, 2), 
           boost::asio::transfer_at_least(2), 
**ERROR POINTING THIS LINE**         cb); 
     } 

     void Command::got_newline() { 
     if (m_newline[0] == '\r' && m_newline[1] == '\n') { 
      std::cout << "End" << std::endl; 
      } 
     } 

    } 
    } 
} 

コードブロックの[**エラーでこの行を指しています**]を確認してください。ここに問題があります。 、何度も何度も頭を破った...私は、明確にする目的のためにいくつかのコードを削除したの助け

ため

感謝は、何か質問がある場合は、*

答えて

3

あなたの完了ハンドラの署名が正しくないと、

#include <boost/asio.hpp> 

#include <boost/function.hpp> 
#include <boost/bind.hpp> 

void 
foo() 
{ 

} 

int 
main() 
{ 
    boost::asio::io_service io_service; 
    boost::asio::ip::tcp::socket socket(io_service); 

    char buf[2]; 

    // this compiles file 
    boost::asio::async_read(
      socket, 
      boost::asio::buffer(buf), 
      boost::asio::transfer_at_least(2), 
      boost::bind(&foo) 
      ); 

    // this does not 
    boost::function<void()> cb = boost::bind(&foo); 
    boost::asio::async_read(
      socket, 
      boost::asio::buffer(buf), 
      boost::asio::transfer_at_least(2), 
      cb 
      ); 

} 

boost::bindこの例を考えて、あなたのバインドされた関数ポインタにerrorまたはbytes_transferredパラメータを渡すことではないのに十分なスマートです。 Asioライブラリの作成者には、ライブラリとのバインドの使用に関するdetailed blog postがあります。それは読む価値があります。

+0

ありがとう!!そしてそれはどうですか、私とほとんど同じようですね。 – TheSquad

+0

答えは1つです。ブログポストへのリンクのために2番目の時間を与えることができれば、今日もまた何かを学んでくれてありがとう^^ –

1

async_を躊躇しないでください操作は、コールバック関数の異なる署名が必要:

void handler(
    const boost::system::error_code& error, // Result of operation. 
    std::size_t bytes_transferred   // Number of bytes read. 
); 

、このようなコールバックハンドラを作成して呼び出す方法をいくつかの例については、ドキュメントでより深く見てください。

+1

これはクラスのshared_ptrです:public shared_from_this <>、他のクラスでもうまくいきます。これはコンパイラが不平を言っているものではありません。ありがとう – TheSquad

+0

残念なことに私はやった、これは私が理解していない理由です...私は単純なvoid()関数を呼び出しています、そして私のコールバックはboost :: functionです ...もっと簡単にすることはできません: - /。 – TheSquad

+0

いいえ、[このページ](http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/reference/async_read/overload1.html)をご覧ください。 void()関数ではなく、コールバックシグネチャが投稿されています.2つのパラメータが必要です。 –

関連する問題