2010-01-28 2 views
4

async_write에 추가 boost :: 함수를 제공하고 싶습니다. 나는 연결 자체가 HandleWrite 함수를 먼저 호출하고 제공된 boost :: function을 호출하기를 원한다. 결합 연결boost :: : boost :: function 매개 변수를 사용하여 메서드를 바인딩하십시오.

멤버 메소드

void Connection::HandleWrite(
    const boost::system::error_code& e, 
    boost::function<void (const boost::system::error_code&)> handler) 
{ 
    // Code removed for clarity 

    if(!handler.empty()) 
     handler(e); 
}; 

는 ASIO async_write에 HandleWrite 묶고 핸들러 값으로서 다른 바인딩을 제공하려고 async_write ASIO한다. 이것은 컴파일되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

Error 1 error C2825: 'F': must be a class or namespace when followed by '::' boost\bind\bind.hpp 69 
Error 2 error C2039: 'result_type' : is not a member of '`global namespace'' boost\bind\bind.hpp 69 
Error 3 error C2146: syntax error : missing ';' before identifier 'type' boost\bind\bind.hpp 69 
Error 4 error C2208: 'boost::_bi::type' : no members defined using this type boost\bind\bind.hpp 69 
Error 5 fatal error C1903: unable to recover from previous error(s); stopping compilation boost\bind\bind.hpp 69 
+1

오류 메시지를 제공하면 도움이 될 것입니다. –

+0

위에 오류 메시지가 추가되지 않았습니다. –

+0

문제는 동일한 HandleWrite 함수를 사용하고 올바르게 바인딩되지 않은 다른 장소에있는 것으로 판명되었습니다. 그것을 수정 한 후에 컴파일되었습니다. –

답변

0

동일한 HandleWrite 함수를 사용하고 올바르게 바인딩되지 않은 다른 곳에서 문제가 발생했습니다. 그것을 수정 한 후에 컴파일되었습니다.

0

어떤 오류 (들)을 정확히지고있다 :

void Connection::QueueRequest(
     boost::shared_array<char> message, 
     std::size_t size, 
     boost::function<void (const boost::system::error_code&)> handler) 
    { 
    // Code hidden for clarity 

    boost::asio::async_write(m_Socket, boost::asio::buffer(buffer), 
     boost::bind(&Connection::HandleWrite, shared_from_this(), 
      boost::asio::placeholders::error, 
      handler 
     ) 
    ); 
    } 

내가 컴파일러에서 얻을 오류 메시지는 다음과 같다? 귀하의 질문에 표시된 코드에서 명백하게 잘못된 것이 아무것도 없으므로 직접 대답을 드릴 수는 없습니다.

그러나 Kornel의 대답은 내가 boost :: bind에 의해 생성 된 펑터가 여러 가지 인수를 취할 수 있고 단순히 여분의 인수를 무시한다고 생각했기 때문에 의심스러워했습니다.

그래서 나는 빨리이 확인 해킹 :

#include <boost/asio.hpp> 
#include <boost/bind.hpp> 
#include <boost/shared_ptr.hpp> 
#include <boost/enable_shared_from_this.hpp> 
#include <boost/function.hpp> 
#include <string> 
#include <iostream> 


void Foo(const boost::system::error_code&) 
{ 
    // whatever 
} 

struct Client : boost::enable_shared_from_this<Client> 
{ 
    void HandleWrite(
     const boost::system::error_code& Err, 
     boost::function<void(const boost::system::error_code&)> OtherHandler 
    ) 
    { 
     std::cout << "MyHandler(" << Err << ")\n"; 
     OtherHandler(Err); 
    } 

    void MakeTheCall(boost::function<void (const boost::system::error_code&)> Other) 
    { 
     using boost::asio::ip::tcp; 

     // Of course, the scope and initialization of 
     // io_service, sock and request are all wrong here, 
     // as we're only interested in testing if the async_write 
     // call below will compile. 
     // Don't try to run this at home! 
     boost::asio::io_service io_service; 
     tcp::socket sock(io_service); 
     boost::asio::streambuf request; 

     boost::asio::async_write(sock, request, 
      boost::bind(&Client::HandleWrite, shared_from_this(), 
       boost::asio::placeholders::error, 
       Other 
      ) 
     ); 
    } 
}; 


int main() 
{ 
    boost::shared_ptr<Client> c; 
    c->MakeTheCall(boost::bind(&Foo, _1)); 

    return 0; 
} 

나는 당신이하려는 생각 무엇을 스케치된다.

예상대로 컴파일되므로 실제로 수행중인 작업과 비교하면 문제를 찾는 데 도움이 될 수 있습니다.

관련 문제