如何在C ++ 11的模板中使用模板? [英] How to use template within template in C++11?

查看:59
本文介绍了如何在C ++ 11的模板中使用模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用boost创建一个websocket服务器.因为我们需要ws和wss两个版本,所以我写下面的代码. 但是当我构建它时,说错误C2338:async_teardown中的未知套接字类型."

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."

我想是因为T1.

文件1 iSession.h '''

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 '''

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

'''

文件3 SslSession.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

'''

我想这是因为我的SslSession与T1模板有关. 因为当SslSession注释时,我的类NormalSession可以运行.

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?

更多消息

更多信息

'''

F:\ boost_1_69_0 \ boost_1_69_0 \ boost/beast/websocket/teardown.hpp(103):错误C2338:async_teardown中的套接字类型未知. F:\ boost_1_69_0 \ boost_1_69_0 \ boost/beast/websocket/impl/read.ipp(680):注意: :mutable_buffers_type,boost :: beast :: websocket :: stream :: read_op> :: process ::,boost :: asio :: strand >>>>(boost :: beast :: websocket :: role_type,Socket& ;, TeardownHandler&)"的引用 和 [ NextLayer = boost :: asio :: ssl :: stream, DynamicBuffer = boost :: beast :: multi_buffer, 套接字= 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 ::股>>> ] F:\ boost_1_69_0 \ boost_1_69_0 \ boost/beast/websocket/impl/read.ipp(173):注意:编译类模板成员函数"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)时 和 [ 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):注意:对正在编译的函数模板实例化"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) "的引用 和 [ 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):注意:对正在编译的类模板实例化"boost :: beast :: websocket :: stream :: read_some_op> :: mutable_buffers_type,boost :: beast :: websocket :: stream :: read_op :: process ::,boost :: asio :: strand >>>的引用 和 [ 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):注意:编译类模板成员函数"void boost :: beast :: websocket :: stream :: read_op :: process :: ,, boost :: asio :: strand >> :: operator()(boost :: beast :: error_code,size_t)时 和 [ 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):注意:对正在编译的函数模板实例化"void boost :: beast :: websocket :: stream :: read_op ::: process ::,boost :: asio :: strand >> :: operator()(boost :: beast :: error_code,size_t)"的引用 和 [ 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):注意:对正在编译的类模板实例化::,boost :: asio :: strand >>的引用 和 [ T = boost :: asio :: ssl :: stream, DynamicBuffer = boost :: beast :: multi_buffer ] f:\ msp \ esdk_icp \ source \ esdk_msp \ msp_s \ src \ websocket \ iSession.h(69):注意:参见对正在编译的函数模板实例化"void boost :: beast :: websocket :: stream :: async_read :: process ::,boost :: asio :: strand >>(DynamicBuffer&,ReadHandler&&))的引用 和 [ 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):注意:参见对正在编译的函数模板实例化"void boost :: beast :: websocket :: stream :: async_read :: process ::,boost :: asio :: strand >>(DynamicBuffer&,ReadHandler&&))的引用 和 [ 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):注意:编译类模板成员函数"void iSession> :: process(void)"时 F:\ MSP \ eSDK_ICP \ Source \ eSDK_MSP \ MSP_S \ src \ websocket \ SslSession.cpp(44):注意: f:\ msp \ esdk_icp \ source \ esdk_msp \ msp_s \ src \ websocket \ SslSession.h(16):注意:参见对正在编译的类模板实例化"iSession>"的引用 F:\ boost_1_69_0 \ boost_1_69_0 \ boost/beast/core/string.hpp(103):注意:参见对正在编译的类模板实例化"boost :: basic_string_view>"的引用 NMAKE:致命错误U1077:"D:\ VS2017 \ VC \ Tools \ MSVC \ 14.16.27023 \ bin \ Hostx64 \ x64 \ cl.exe":返回代码"0x2" 停止. NMAKE:致命错误U1077:"D:\ VS2017 \ VC \ Tools \ MSVC \ 14.16.27023 \ bin \ Hostx64 \ x64 \ nmake.exe":返回代码"0x2" 停止. NMAKE:致命错误U1077:"D:\ VS2017 \ VC \ Tools \ MSVC \ 14.16.27023 \ bin \ Hostx64 \ x64 \ nmake.exe":返回代码"0x2" 停止. NMAKE:致命错误U1077:"D:\ VS2017 \ VC \ Tools \ MSVC \ 14.16.27023 \ bin \ Hostx64 \ x64 \ nmake.exe":返回代码"0x2" 停止.

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.

'''

推荐答案

理解它,在阅读源代码之后,我阅读了teardown.hpp中的注释,忘记了包含#include <boost/beast/websocket/ssl.hpp>.

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

这篇关于如何在C ++ 11的模板中使用模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆