【问题标题】:No matching constructor error (Boost Beast, C++)没有匹配的构造函数错误(Boost Beast,C++)
【发布时间】:2021-03-03 10:29:19
【问题描述】:

这是我的 WebSocket 客户端的头文件:

namespace beast = boost::beast;
namespace asio = boost::asio;

class WebSocketClient {
 public:
  explicit WebSocketClient(asio::io_context &ioc);

 private:
  using stream_type = beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>;
  using ssl_context_type = asio::ssl::context;

  asio::ip::tcp::resolver resolver_;
  stream_type ws_;

  ssl_context_type get_ssl_context();
};

这是我的定义:

WebSocketClient::WebSocketClient(asio::io_context &ioc)
    : resolver_(asio::make_strand(ioc)),
      ws_(asio::make_strand(ioc), get_ssl_context()) {
}

asio::ssl::context WebSocketClient::get_ssl_context() {
  return WebSocketClient::ssl_context_type(asio::ssl::context_base::tlsv12_client);
}

我收到了错误:

In template: no matching constructor for initialization of 'boost::beast::ssl_stream<boost::beast::basic_stream<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::execution::context_as_t<boost::asio::execution_context &>, boost::asio::execution::detail::blocking::never_t<0>, boost::asio::execution::prefer_only<boost::asio::execution::detail::blocking::possibly_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::tracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::outstanding_work::untracked_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::fork_t<0>>, boost::asio::execution::prefer_only<boost::asio::execution::detail::relationship::continuation_t<0>>>, boost::beast::unlimited_rate_policy>>' error occurred here in instantiation of function template specialization 'boost::empty_::empty_value<boost::beast::ssl_stream<boost::beast::basic_stream<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::execution::context_as_t<boost::... in instantiation of function template specialization 'boost::beast::websocket::stream<boost::beast::ssl_stream<boost::beast::basic_stream<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::execution::context_as_t<bo... in instantiation of function template specialization 'boost::make_shared<boost::beast::websocket::stream<boost::beast::ssl_stream<boost::beast::basic_stream<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::executi... in instantiation of function template specialization 'boost::beast::websocket::stream<boost::beast::ssl_stream<boost::beast::basic_stream<boost::asio::ip::tcp, boost::asio::execution::any_executor<boost::asio::execution::context_as_t<bo... candidate constructor template not viable: expects an l-value for 2nd argument candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided

如果有人能解释为什么我会收到此错误,我将不胜感激?如果我改为使用下面的代码,并添加另一个类型为 ssl_context_type 的私有成员变量 ssl_context_,错误就会消失,但我不确定为什么......我对 C++ 还是很陌生。

WebSocketClient::WebSocketClient(asio::io_context &ioc)
    : resolver_(asio::make_strand(ioc)),
      ssl_context_(asio::ssl::context_base::tlsv12_client),
      ws_(asio::make_strand(ioc), ssl_context_) {
}

【问题讨论】:

  • 这是完整的错误信息吗?编译器通常会给你一些关于它不喜欢什么的提示
  • 我已更新以提供完整的错误消息

标签: c++ boost boost-asio initializer-list boost-beast


【解决方案1】:

问题在于boost::asio::ssl::stream 类构造函数(通过归纳,beast::websocket::stream&lt;beast::ssl_stream&lt;beast::tcp_stream&gt;&gt; 构造函数)需要boost::asio::ssl::contextl-value 作为第二个参数,而WebSocketClient::get_ssl_context 返回一个r-value。换句话说,WebSocketClient::get_ssl_context 返回一个临时值,这对于 beast::websocket::stream&lt;beast::ssl_stream&lt;beast::tcp_stream&gt;&gt; 是不可接受的。

ssl::context 需要在 TLS 流的整个持续时间内持续存在,因为流将在其操作期间使用上下文。所以正确的解决方案是创建一个ssl::context 的实例作为WebSocketClient 的数据成员之前 ws_ 并提供对ws_ 构造函数的引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2014-11-03
    • 2022-01-15
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多