c++:如何在构造函数中初始化 std::pair 的成员
我有以下课程:
typedef std::pair<boost::asio::ip::tcp::socket, boost::asio::ip::tcp::socket> socket_pair;
class ConnectionPair {
private:
socket_pair _sockPair;
public:
ConnectionPair(boost::asio::io_service &ios);
}
如何在构造函数中初始化成对的套接字?以下内容将无法编译:
ConnectionPair::ConnectionPair(asio::io_service &ios):
_ios(ios),
_sockPair(asio::ip::tcp::socket(ios), asio::ip::tcp::socket(ios)){
}
编辑:这是编译器错误。享受:
/boost_1_47_0/boost/asio/basic_io_object.hpp: In copy constructor ‘boost::asio::basic_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >::basic_socket(const boost::asio::basic_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >&)’:
/boost_1_47_0/boost/asio/basic_socket.hpp:43:1: instantiated from ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = boost::asio::basic_stream_socket<boost::asio::ip::tcp>, _T2 = boost::asio::basic_stream_socket<boost::asio::ip::tcp>]’
/devel/msm1/connection.cpp:8:67: instantiated from here
/boost_1_47_0/boost/asio/basic_io_object.hpp:163:3: error: ‘boost::asio::basic_io_object<IoObjectService>::basic_io_object(const boost::asio::basic_io_object<IoObjectService>&) [with IoObjectService = boost::asio::stream_socket_service<boost::asio::ip::tcp>, boost::asio::basic_io_object<IoObjectService> = boost::asio::basic_io_object<boost::asio::stream_socket_service<boost::asio::ip::tcp> >]’ is private
/boost_1_47_0/boost/asio/basic_socket.hpp:43:1: error: within this context
In file included from /boost_1_47_0/boost/asio.hpp:30:0,
I have the following class:
typedef std::pair<boost::asio::ip::tcp::socket, boost::asio::ip::tcp::socket> socket_pair;
class ConnectionPair {
private:
socket_pair _sockPair;
public:
ConnectionPair(boost::asio::io_service &ios);
}
How do I init the sockets in the pair in the constructor ? the following won't compile:
ConnectionPair::ConnectionPair(asio::io_service &ios):
_ios(ios),
_sockPair(asio::ip::tcp::socket(ios), asio::ip::tcp::socket(ios)){
}
EDIT: Here is the compiler error. Enjoy:
/boost_1_47_0/boost/asio/basic_io_object.hpp: In copy constructor ‘boost::asio::basic_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >::basic_socket(const boost::asio::basic_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >&)’:
/boost_1_47_0/boost/asio/basic_socket.hpp:43:1: instantiated from ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = boost::asio::basic_stream_socket<boost::asio::ip::tcp>, _T2 = boost::asio::basic_stream_socket<boost::asio::ip::tcp>]’
/devel/msm1/connection.cpp:8:67: instantiated from here
/boost_1_47_0/boost/asio/basic_io_object.hpp:163:3: error: ‘boost::asio::basic_io_object<IoObjectService>::basic_io_object(const boost::asio::basic_io_object<IoObjectService>&) [with IoObjectService = boost::asio::stream_socket_service<boost::asio::ip::tcp>, boost::asio::basic_io_object<IoObjectService> = boost::asio::basic_io_object<boost::asio::stream_socket_service<boost::asio::ip::tcp> >]’ is private
/boost_1_47_0/boost/asio/basic_socket.hpp:43:1: error: within this context
In file included from /boost_1_47_0/boost/asio.hpp:30:0,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果该类型是可复制构造的,那么您的代码就可以工作。我猜测(并且只是猜测,因为您没有指定编译器错误)套接字不可复制构造。由于 std::pair 不允许就地工厂,因此您必须使您的对成为一对 boost::Optional 并使用就地工厂。请参阅boost 文档了解更多细节。
If the type is copy-constructible, your code would have worked. I guess (and only guess, because you didn't specify the compiler error) that a socket is not copy-constructible. Since std::pair does not allow in-place factories, you'll have to make your pair a pair of boost::optional's and use in-place factories. See the boost documentation for more details.
boost::asio::ip::tcp::socket
支持复制吗?我没想到会这样。并且
std::pair
中的类型必须是可复制的。Does
boost::asio::ip::tcp::socket
support copy? I wouldn't expect it.And types in an
std::pair
must be copyable.您可以像尝试执行的那样初始化该对,但是代码中还有一些其他错误。
不幸的是我没有安装 boost,但这可以使用 G++ 4.1.2 为我编译
You can initialize the pair like you are trying to do, but you have a couple of other errors in your code.
Unfortunately I don't have boost installed, but this compiles for me using G++ 4.1.2