没有匹配的构造函数错误(Boost Beast,C++)
No matching constructor error (Boost Beast, C++)
这是我的 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_) {
}
问题是 boost::asio::ssl::stream
class 构造函数(通过归纳,beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>
构造函数)需要一个 l-value of boost::asio::ssl::context
as the second parameter, and WebSocketClient::get_ssl_context
returns an r-value。换句话说,WebSocketClient::get_ssl_context
returns 是临时的,beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>
.
是不可接受的
ssl::context
需要在 TLS 流的整个持续时间内持续存在,因为流将在其操作期间使用上下文。所以正确的解决方案是在 ws_
之前创建一个ssl::context
的实例作为WebSocketClient
的数据成员,并提供对它的引用给ws_
构造函数。
这是我的 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_) {
}
问题是 boost::asio::ssl::stream
class 构造函数(通过归纳,beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>
构造函数)需要一个 l-value of boost::asio::ssl::context
as the second parameter, and WebSocketClient::get_ssl_context
returns an r-value。换句话说,WebSocketClient::get_ssl_context
returns 是临时的,beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>
.
ssl::context
需要在 TLS 流的整个持续时间内持续存在,因为流将在其操作期间使用上下文。所以正确的解决方案是在 ws_
之前创建一个ssl::context
的实例作为WebSocketClient
的数据成员,并提供对它的引用给ws_
构造函数。