使用 boost::asio::async_read 读入缓冲区时出现问题
再会。
我的项目中有一个 Types.hpp 文件。在其中我有:
....
namespace RC
{
.....
.....
struct ViewSettings
{
....
};
.....
}
在 Server.cpp 文件中,我包含了这个 Types.hpp 文件,并且我在那里:
class Session
{
.....
RC::ViewSettings tmp;
boost::asio::async_read(socket_, boost::asio::buffer(&tmp, sizeof(RC::ViewSettings)),
boost::bind(&Session::Finish_Reading_Data, shared_from_this(), boost::asio::placeholders::error));
.....
}
在编译过程中我遇到了错误:
error C2825: 'F': must be a class or namespace when followed by '::'
: see reference to class template instantiation 'boost::_bi::result_traits<R,F>'
being compiled with
[
R=boost::_bi::unspecified,
F=void (__thiscall Session::* )(void)
]
: see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>'
being compiled with
[
R=boost::_bi::unspecified,
F=void (__thiscall Session::* )(void),
L=boost::_bi::list2<boost::_bi::value<boost::shared_ptr<Session>>,boost::arg<1>>
]
error C2039: 'result_type' : is not a member of '`global namespace''
像这样的代码以正确的方式工作:
int w;
boost::asio::async_read(socket_, boost::asio::buffer(&w, sizeof(int)),
boost::bind(&Session::Handle_Read_Width, shared_from_this(), boost::asio::placeholders::error));
请帮忙。这里有什么问题? 提前致谢。
Good day.
I have a Types.hpp file in my project. And within it i have:
....
namespace RC
{
.....
.....
struct ViewSettings
{
....
};
.....
}
In the Server.cpp file I'm including this Types.hpp file, and i have there:
class Session
{
.....
RC::ViewSettings tmp;
boost::asio::async_read(socket_, boost::asio::buffer(&tmp, sizeof(RC::ViewSettings)),
boost::bind(&Session::Finish_Reading_Data, shared_from_this(), boost::asio::placeholders::error));
.....
}
And during the compilation i have an errors:
error C2825: 'F': must be a class or namespace when followed by '::'
: see reference to class template instantiation 'boost::_bi::result_traits<R,F>'
being compiled with
[
R=boost::_bi::unspecified,
F=void (__thiscall Session::* )(void)
]
: see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>'
being compiled with
[
R=boost::_bi::unspecified,
F=void (__thiscall Session::* )(void),
L=boost::_bi::list2<boost::_bi::value<boost::shared_ptr<Session>>,boost::arg<1>>
]
error C2039: 'result_type' : is not a member of '`global namespace''
And the code like this works in proper way:
int w;
boost::asio::async_read(socket_, boost::asio::buffer(&w, sizeof(int)),
boost::bind(&Session::Handle_Read_Width, shared_from_this(), boost::asio::placeholders::error));
Please, help. What's the problem here?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要从
boost::enable_shared_from_this继承 Session。
You need to inherit Session from
boost::enable_shared_from_this<Session>.
您正在尝试传输一个结构,而不仅仅是一个简单的类型。这就是为什么它适用于 int,而不适用于 struct。
我认为这是序列化领域,所以我认为你需要 boost.serialization。
http://www.boost.org/doc/ libs/1_43_0/doc/html/boost_asio/examples.html
检查这个序列化示例,也许会对您有所帮助。
You are trying to transfer a struct, not just a simple type. That's why it works with int, and won't work with the struct.
I think this is in the area of serialization, so i think you will need boost.serialization.
http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/examples.html
Check this serialization example, maybe that will help you out.