如何在C ++ 11的模板中使用模板?

时间:2019-05-15 11:05:29

标签: c++ c++11 templates boost

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

我想是因为T1。

文件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

'''

文件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可以运行。

那么如何解决此错误以及如何在模板中使用模板?

更多消息

  

更多信息

'''

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):注意:对正在编译的函数模板实例化“ 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 &&)”的引用         与         [             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 :: beast :: websocket :: stream :: read_op :: process ::,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):注意:参见对正在编译的函数模板实例化“ void iSession> :: process(void)”的引用 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” 停止。

'''

1 个答案:

答案 0 :(得分:0)

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