我有以下课程:
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,
答案 0 :(得分:4)
如果类型是可复制构造的,那么您的代码就可以工作了。我猜(并且只是猜测,因为你没有指定编译器错误)套接字不是可复制构造的。由于std :: pair不允许就地工厂,你必须使你的对成为一对boost :: optional并使用就地工厂。有关详细信息,请参阅the boost documentation。
答案 1 :(得分:3)
boost::asio::ip::tcp::socket
是否支持复制?我不指望它。
std::pair
中的类型必须是可复制的。
答案 2 :(得分:0)
您可以像尝试一样初始化该对,但您的代码中还有其他一些错误。
不幸的是我没有安装boost,但这使用G ++ 4.1.2编译我的
#include <utility>
typedef std::pair<int, int> socket_pair;
class ConnectionPair
{
private:
socket_pair _sockPair;
public:
ConnectionPair(const int x);
};
ConnectionPair::ConnectionPair(const int x):
_sockPair(x, x)
{
}
int main(int argc, const char *argv[])
{
ConnectionPair c(10);
}