【问题标题】:Error in xutility when trying to run program C++尝试运行程序 C++ 时 xutility 出错
【发布时间】:2021-08-20 04:08:32
【问题描述】:

我正在尝试为名为 aegis 的库运行示例程序,但是当我尝试编译该程序时出现错误

\vstudio\vide\VC\Tools\MSVC\14.29.30037\include\xutility(158,56): error C2664: 'asio::basic_stream_socket<asio::ip::tcp,asio::any_io_executor>::basic_stream_socket<_Ty>(ExecutionContext &,int)': cannot convert argument 1 from '_Ty' to 'ExecutionContext &'
with
         [
             _Ty=std::reference_wrapper<asio::io_context>,
             ExecutionContext=std::reference_wrapper<asio::io_context>
         ]
         and
         [
             _Ty=std::reference_wrapper<asio::io_context>
         ]
         and
         [
             ExecutionContext=std::reference_wrapper<asio::io_context>
         ]

我会尝试自己修复它,但我不知道从哪里开始。我的主文件中的代码是:

#define _WINSOCKAPI_
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#include <windows.h>
#include <iostream>
#include <aegis.hpp>

int main() {

    aegis::core bot(aegis::create_bot_t().log_level(spdlog::level::trace).token("NzB3Bjk2MjJF1LK0NzYxNzIx.VBi9lN.tj21KhNBMv8_3hzt9ha3LKK9j5fI"));
    AEGIS_TRACE(bot.log, "Bot object created");
    // With min log level set to trace and wsdbg (websocket debug) set to true
    // the bot will dump all websocket messages to console
    bot.wsdbg = true;
    // These callbacks are what are called when websocket events occur
    bot.set_on_message_create([&](aegis::gateway::events::message_create obj)
        {
            std::string content{ obj.msg.get_content() };
            auto& _channel = obj.msg.get_channel();
            // Simple Hi response
            if (content == "~Hi")
            {
                _channel.create_message("Hello back");
            }
        });
    // start the bot
    bot.run();
    // yield thread execution to the library
    bot.yield();
    std::cout << "Press any key to continue...\n";
    std::cin.ignore();
    return 0;
}

这是错误所在的代码行:

#endif // __cpp_lib_constexpr_dynamic_alloc
    {
        ::new (_Voidify_iter(_STD addressof(_Obj))) _Ty(_STD forward<_Types>(_Args)...);
    }

错误后的信息是:

D:\vstudio\vide\VC\Tools\MSVC\14.29.30037\include\xutility(158,77): message : A non-const reference may only be bound to an lvalue
 D:\project\asio-master\asio-master\asio\include\asio\basic_stream_socket.hpp(113,3): message : see declaration of 'asio::basic_stream_socket<asio::ip::tcp,asio::any_io_executor>::basic_stream_socket'
 D:\vstudio\vide\VC\Tools\MSVC\14.29.30037\include\memory(2021): message : see reference to function template instantiation 'void std::_Construct_in_place<_Ty,std::reference_wrapper<asio::io_context>>(_Ty &,std::reference_wrapper<asio::io_context> &&) noexcept(false)' being compiled
         with
         [
             _Ty=asio::ip::tcp::socket
         ]
 D:\vstudio\vide\VC\Tools\MSVC\14.29.30037\include\memory(2747): message : see reference to function template instantiation 'std::_Ref_count_obj2<_Ty>::_Ref_count_obj2<std::reference_wrapper<asio::io_context>>(std::reference_wrapper<asio::io_context> &&)' being compiled
         with
         [
             _Ty=asio::ip::tcp::socket
         ]
 D:\vstudio\vide\VC\Tools\MSVC\14.29.30037\include\memory(2748): message : see reference to function template instantiation 'std::_Ref_count_obj2<_Ty>::_Ref_count_obj2<std::reference_wrapper<asio::io_context>>(std::reference_wrapper<asio::io_context> &&)' being compiled
         with
         [
             _Ty=asio::ip::tcp::socket
         ]
 D:\project\websocketpp-master\websocketpp-master\websocketpp\transport\asio\security\none.hpp(172): message : see reference to function template instantiation 'std::shared_ptr<websocketpp::transport::asio::basic_socket::connection::socket_type> std::make_shared<asio::ip::tcp::socket,std::reference_wrapper<asio::io_context>>(std::reference_wrapper<asio::io_context> &&)' being compiled

【问题讨论】:

  • 该错误不是因为您的程序无法运行。这是一个编译器错误——您的程序尚未成功构建。
  • @PaulMcKenzie 是的,我明白了,我该如何解决这个错误?
  • 查看代码中错误的来源。如果您使用的是 Visual Studio,则错误输出将或应该最终指向代码中产生错误的行。但老实说,如果您不了解 C++,那么您在尝试找出重要的 C++ 错误时不会有任何帮助。
  • 这些类型的错误通常在之前或之后打印更多的上下文。例如,在这种情况下,它应该说明试图替换 _Ty 的类型
  • @PeterT 谢谢我明白你的意思,我已经把它添加到帖子中了。

标签: c++ visual-studio websocket


【解决方案1】:

您正在使用不兼容的 boost asio 和 websocketpp 版本。

此兼容性问题已在 websocketpp 中修复:https://github.com/zaphoyd/websocketpp/commit/c769c9238ad62178f506038178714a1c35aa2769#diff-4f2d1c5e172580e501cd2f4de10a0faffe48c0bb673a6720733bc7d1459da5f4L171

解决此问题的最简单方法是更新 websocketpp。

【讨论】:

  • 修复了错误,但现在我有来自 asio 的错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 2017-07-15
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 1970-01-01
相关资源
最近更新 更多