我试图通过使用Boost:asio库进行一些网络编程来感受我的方式。我一直在使用异步处理程序从给定地址传输数据,当我将asio用作函数时,它工作正常:
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver( io_service);
boost::asio::ip::tcp::socket sock( io_service);
boost::array < char, 4096 > buffer;
void read_handler( const boost:: system:: error_code &ec,
std:: size_t bytes_transferred)
{
if (! ec)
{
if (std::string(buffer.data(), bytes_transferred) == "$")
{
myss.str(std::string());
}
myss << std::string(buffer.data(), bytes_transferred);
if (int(buffer.data()[0]) == 10)
{
....
}
std::cout << std::string(buffer.data(), bytes_transferred) ;//<< std::endl;
sock.async_read_some( boost::asio::buffer( buffer), read_handler);
}
}
void connect_handler( const boost::system::error_code &ec)
{
if (! ec)
{
boost::asio::write( sock, boost::asio::buffer(
"GET / HTTP 1.1\r\nHost: www.google.com\r\n\r\n"));
sock.async_read_some( boost::asio::buffer( buffer), read_handler);
}
}
void resolve_handler( const boost::system::error_code &ec,
boost:: asio::ip::tcp::resolver::iterator it)
{
if (! ec)
{
sock.async_connect(* it, connect_handler);
}
}
现在,我正在尝试将这些函数集成到一个类中,但Visual Studio一直在抱怨我在类中实现函数的方式。
void
MyClass::connect_handler( const boost::system::error_code &ec)
{
if (! ec)
{
//boost::asio::write( sock, boost::asio::buffer(
// "GET / HTTP 1.1\r\nHost: www.highscore.de\r\n\r\n"));
m_sock.async_read_some( boost::asio::buffer( m_buffer), read_handler);
}
}
: error C3867: 'MyClass::read_handler': function call missing argument list; use '&MyClass::read_handler' to create a pointer to member:
error C3867: 'MyClass::read_handler': function call missing argument list; use '&MyClass::read_handler' to create a pointer to member
2>'MyClass::connect_handler': function call missing argument list; use
'&MyClass::connect_handler' to create a pointer to member
2>
2>Build FAILED.
任何人都有任何关于我将错误移到课堂上的错误的指示?
我已尝试在错误消息中实施建议的更改,但它会导致boost库本身出错。
2>C:\installs\Boost\include\boost-1_49\boost/asio/basic_stream_socket.hpp(785): error C2338: ReadHandler type requirements not met
2> : see reference to function template instantiation 'void boost::asio::basic_stream_socket<Protocol>::async_read_some<boost::asio::mutable_buffers_1,void(__thiscall MyClass::Sensor::* )(const boost::system::error_code &,size_t)>(const MutableBufferSequence &,const ReadHandler &)' being compiled
2> with
2> [
2> Protocol=boost::asio::ip::tcp,
2> MutableBufferSequence=boost::asio::mutable_buffers_1,
2> ReadHandler=void (__thiscall MyClass::Sensor::* )(const boost::system::error_code &,size_t)
2> ]
2>C:\installs\Boost\include\boost-1_49\boost/asio/basic_stream_socket.hpp(785): error C2064: term does not evaluate to a function taking 2 arguments
2>C:\installs\Boost\include\boost-1_49\boost/asio/basic_socket.hpp(706): error C2338: ConnectHandler type requirements not met
2> : see reference to function template instantiation 'void boost::asio::basic_socket<Protocol,SocketService>::async_connect<void(__thiscall MyClass::Sensor::* )(const boost::system::error_code &)>(const boost::asio::ip::basic_endpoint<InternetProtocol> &,const ConnectHandler &)' being compiled
2> with
2> [
2> Protocol=boost::asio::ip::tcp,
2> SocketService=boost::asio::stream_socket_service<boost::asio::ip::tcp>,
2> InternetProtocol=boost::asio::ip::tcp,
2> ConnectHandler=void (__thiscall MyClass::Sensor::* )(const boost::system::error_code &)
2> ]
2>C:\installs\Boost\include\boost-1_49\boost/asio/basic_socket.hpp(706): error C2064: term does not evaluate to a function taking 1 arguments
2>
答案 0 :(得分:5)
m_sock.async_read_some(boost :: asio :: buffer(m_buffer),read_handler);
假设read_handler
是一个接受2个参数的成员函数,它应该是:m_sock.async_read_some(boost::asio::buffer( m_buffer), boost::bind(&MyClass::read_handler, this, _1, _2));
如果您想使用shared_from_this
成语(即您的类继承自enable_shared_from_this
),请在shared_from_this()
表达式中使用this
代替bind
。< / p>
答案 1 :(得分:1)
您只能通过命名成员来创建指向成员的指针,您需要使用类限定显式请求它:
&MyClass::connect_handler
(由于错误信息非常明确地说明......)