2011-06-28 102 views
1

我想不通,爲什麼我得到這個錯誤:的boost ::支持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; 
      } 
     } 

    } 
    } 
} 

檢查代碼塊上的「** Error pointing this line **」,這是它存在問題的原因......不知道爲什麼,一次又一次打破了我的頭......

感謝您的幫助

我已刪除了清楚起見,一些代碼,如果你有任何問題,請不要猶豫

回答

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是足夠聰明的errorbytes_transferred參數不傳遞給您綁定的函數指針。 Asio圖書館的作者有一個detailed blog post關於使用綁定與圖書館。這是值得的閱讀。

+0

謝謝!那怎麼樣,對我來說看起來差不多,不是嗎? – TheSquad

+0

一個+1的答案,如果我可以,我會給你第二個鏈接到博客帖子,謝謝,今天又學到了一些東西^^ –

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()函數,它需要兩個參數! –

相關問題