How to use template within template in C++11?

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I use boost to create a websocket server. Because we need ws and wss, two versions, so I write code like below. But when I build it, said "error C2338: Unknown Socket type in async_teardown."

I guess it's becuuse T1 .

file 1 iSession.h '''

// // Created by p00451414 on 2019/5/14. //  #ifndef ESDK_MSP_ISESSION_H #define ESDK_MSP_ISESSION_H #pragma once #include "stdfax.h"   template<typename T> class iSession :public std::enable_shared_from_this <iSession<T>> { public:     /*explicit         iSession(boost::asio::ip::tcp::socket);     explicit         iSession(boost::asio::ip::tcp::socket, boost::asio::ssl::context&);*/      iSession() {         ws_ = NULL;         strand_ = NULL;         buffer_ = new boost::beast::multi_buffer();     }     ~iSession() {         delete buffer_;         buffer_ = NULL;         delete strand_;         strand_ = NULL;         delete ws_;         ws_ = NULL;      }  public:     boost::beast::websocket::stream<T> *ws_=NULL;     boost::asio::strand<boost::asio::io_context::executor_type> *strand_=NULL;     boost::beast::multi_buffer *buffer_=NULL; public:     void virtual run() = 0;      void process() {         auto sp = shared_from_this();          ws_->async_read(*buffer_, boost::asio::bind_executor(*strand_, [&, sp](boost::system::error_code ec2,             std::size_t bytes_transferred2) {             boost::ignore_unused(bytes_transferred2);             if (ec2) {                 //   fail(ec, "read");             }               if (ec2 == boost::beast::websocket::error::closed) {                 return;             }             ws_->text(ws_->got_text());             ws_->async_write(buffer_->data(), boost::asio::bind_executor(*strand_, [&, sp](boost::system::error_code ec3,                 std::size_t bytes_transferred3) {                 boost::ignore_unused(bytes_transferred3);                 if (ec3) {                     //   return fail(ec, "write");                 }                   // Clear the buffer                 buffer_->consume(buffer_->size());                 process();             }));          }));     }  };   #endif //ESDK_MSP_ISESSION_H 

'''

file2 NormalSession.h '''

// // Created by p00451414 on 2019/5/14. //  #ifndef ESDK_MSP_NORMALSESSION_H #define ESDK_MSP_NORMALSESSION_H    #include "iSession.h"  class NormalSession : public iSession<boost::asio::ip::tcp::socket> { public:     NormalSession(boost::asio::ip::tcp::socket);      ~NormalSession();  public:       void run(); };   #endif //ESDK_MSP_NORMALSESSION_H 

'''

file 3 SslSession.h '''

// // Created by p00451414 on 2019/5/14. //  #ifndef ESDK_MSP_SSLSESSION_H #define ESDK_MSP_SSLSESSION_H   #include <memory> #include <boost/asio/ip/tcp.hpp>   #include "iSession.h"   class SslSession : public iSession<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>> { public:     SslSession(boost::asio::ip::tcp::socket, boost::asio::ssl::context &);      ~SslSession(void);  public:     void on_handshake(boost::system::error_code);      //void on_accept(boost::system::error_code);      //void do_read();      //void on_read(boost::system::error_code, std::size_t);      //void on_write(boost::system::error_code, std::size_t);      void run();  private:     boost::asio::ip::tcp::socket* socket_; };   #endif //ESDK_MSP_SSLSESSION_H 

'''

I guess it's becuuse my SslSession with T1 template. Because my class NormalSession could run when SslSession commented.

So how to fix this error and how to use template within template?

more Msg

more msg

'''

F:\boost_1_69_0\boost_1_69_0\boost/beast/websocket/teardown.hpp(103): error C2338: Unknown Socket type in async_teardown. F:\boost_1_69_0\boost_1_69_0\boost/beast/websocket/impl/read.ipp(680): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::async_teardown::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream::read_op>::process::,boost::asio::strand>>>>(boost::beast::websocket::role_type,Socket &,TeardownHandler &&)”的引用 with [ NextLayer=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer, Socket=boost::asio::ssl::stream, TeardownHandler=boost::beast::websocket::stream,true>::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream,true>::read_op>::process::,boost::asio::strand>>> ] F:\boost_1_69_0\boost_1_69_0\boost/beast/websocket/impl/read.ipp(173): note: 编译 类 模板 成员函数 "void boost::beast::websocket::stream::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>>::operator ()(boost::beast::error_code,size_t,bool)" 时 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:\boost_1_69_0\boost_1_69_0\boost/beast/websocket/impl/read.ipp(821): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::stream::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>>::operator ()(boost::beast::error_code,size_t,bool)”的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:\boost_1_69_0\boost_1_69_0\boost/beast/websocket/impl/read.ipp(821): note: 参见对正在编译的 类 模板 实例化 "boost::beast::websocket::stream::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>>" 的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:\boost_1_69_0\boost_1_69_0\boost/beast/websocket/impl/read.ipp(802): note: 编译 类 模板 成员函数 "void boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>::operator ()(boost::beast::error_code,size_t)" 时 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:\boost_1_69_0\boost_1_69_0\boost/beast/websocket/impl/read.ipp(899): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>::operator ()(boost::beast::error_code,size_t)”的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:\boost_1_69_0\boost_1_69_0\boost/beast/websocket/impl/read.ipp(899): note: 参见对正在编译的 类 模板 实例化 "boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>" 的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] f:\msp\esdk_icp\source\esdk_msp\msp_s\src\websocket\iSession.h(69): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::stream::async_read::process::,boost::asio::strand>>(DynamicBuffer &,ReadHandler &&)”的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer, ReadHandler=boost::asio::executor_binder>::process::,boost::asio::strand> ] f:\msp\esdk_icp\source\esdk_msp\msp_s\src\websocket\iSession.h(44): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::stream::async_read::process::,boost::asio::strand>>(DynamicBuffer &,ReadHandler &&)”的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer, ReadHandler=boost::asio::executor_binder>::process::,boost::asio::strand> ] f:\msp\esdk_icp\source\esdk_msp\msp_s\src\websocket\iSession.h(41): note: 编译 类 模板 成员函数 "void iSession>::process(void)" 时 F:\MSP\eSDK_ICP\Source\eSDK_MSP\MSP_S\src\websocket\SslSession.cpp(44): note: 参见对正在编译的函数 模板 实例化“void iSession>::process(void)”的引用 f:\msp\esdk_icp\source\esdk_msp\msp_s\src\websocket\SslSession.h(16): note: 参见对正在编译的 类 模板 实例化 "iSession>" 的引用 F:\boost_1_69_0\boost_1_69_0\boost/beast/core/string.hpp(103): note: 参见对正在编译的 类 模板 实例化 "boost::basic_string_view>" 的引用 NMAKE : fatal error U1077: “D:\VS2017\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe”: 返回代码“0x2” Stop. NMAKE : fatal error U1077: “D:\VS2017\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\nmake.exe”: 返回代码“0x2” Stop. NMAKE : fatal error U1077: “D:\VS2017\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\nmake.exe”: 返回代码“0x2” Stop. NMAKE : fatal error U1077: “D:\VS2017\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\nmake.exe”: 返回代码“0x2” Stop.

'''

回答1:

Got it ,after read the source code, I read the comments in teardown.hpp, I forget include #include <boost/beast/websocket/ssl.hpp>.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!