【问题标题】:C++ Boost 1.72.0 - 'defer': is not a member of 'std::shared_ptr<boost::asio::io_context>'C++ Boost 1.72.0 - 'defer': 不是 'std::shared_ptr<boost::asio::io_context>' 的成员
【发布时间】:2020-08-11 05:59:10
【问题描述】:

我正在尝试使用 Boost 的 ASIO 库在 Windows 上为我的项目轻松联网,但每当我包含与 std::shared_ptrboost::asio::ip::tcp::acceptor 相关的任何内容时,我都会收到此错误:

'defer': is not a member of 'std::shared_ptr&lt;boost::asio::io_context&gt;'

bool ChatterboxService::Service::open()
{
    context = std::make_shared<boost::asio::io_context>();

    try
    {
        acceptor = std::make_shared<boost::asio::ip::tcp::acceptor>(context, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
    }
    catch (boost::system::system_error error)
    {
        std::cerr << error.what() << std::endl;
        return false;
    }

    return true;
}

我已经尝试删除、重新提取和重新编译整个库,但这并没有什么不同。我已经用谷歌搜索这个问题几个小时了,似乎没有其他人遇到过这个问题。非常感谢任何帮助。

【问题讨论】:

    标签: c++ boost boost-asio shared-ptr


    【解决方案1】:

    您正在尝试从 shared_ptr 构造接受器,您应该取消引用它。

    try
    {
        acceptor = std::make_shared<boost::asio::ip::tcp::acceptor>(*context, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
    }
    

    【讨论】:

    • 啊,好吧,这就解释了。我想它不会出现在 Google 上是有道理的,因为这是一个指针问题,而不是 Boost 问题。所以你可以取消引用 shared_ptr 就好像它是一个常规指针?
    • 是的,您可以取消引用以获取底层对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2019-02-21
    相关资源
    最近更新 更多