c++:如何在构造函数中初始化 std::pair 的成员

发布于 2024-12-06 02:28:26 字数 1789 浏览 2 评论 0原文

我有以下课程:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海 2024-12-13 02:28:26

如果该类型是可复制构造的,那么您的代码就可以工作。我猜测(并且只是猜测,因为您没有指定编译器错误)套接字不可复制构造。由于 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.

看海 2024-12-13 02:28:26

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.

狼性发作 2024-12-13 02:28:26

您可以像尝试执行的那样初始化该对,但是代码中还有一些其他错误。

  • 类声明末尾没有分号。
  • _ios 成员变量不存在。

不幸的是我没有安装 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);
}

You can initialize the pair like you are trying to do, but you have a couple of other errors in your code.

  • No semi colon on the end of your class declaration.
  • _ios member variable does not exist.

Unfortunately I don't have boost installed, but this compiles for me using 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文