【问题标题】:C++: passing the template callback method into the other method: cannot convert ‘<unresolved overloaded function type> to pointerC ++:将模板回调方法传递给其他方法:无法将'<未解析的重载函数类型>转换为指针
【发布时间】:2021-04-06 17:51:15
【问题描述】:

我有一个容器来存储包含模板回调的结构。 同时回调签名不包含模板参数。 插入时出现错误:

cannot convert ‘&lt;unresolved overloaded function type&gt;’ to ‘smb1_subdecoder::send_subflow_premature_end_msg_t’ {aka ‘void (smb1_subdecoder::*)(long unsigned int, unsigned char, long unsigned int, unsigned char)’}

note:   initializing argument 4 of  
‘void smb1_subdecoder::store_subflow_bunch_on_open(
    smb1_subdecoder::handle_t, uint64_t, CIFS_RESOURCE_TYPE, 
    smb1_subdecoder::send_subflow_premature_end_msg_t, 
    smb1_subdecoder::send_subflow_premature_end_msg_t)’
    send_subflow_premature_end_msg_t send_subflow_premature_end_rq_msg,
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

插入代码(此处出错):

store_subflow_bunch_on_open(
    handle(response_msg.file_id), response_msg.msg_id, 
    static_cast<CIFS_RESOURCE_TYPE>(parm_open2_rsp->resource_type),
    &send_subflow_premature_end_msg<ntdec_cifs_close_request>,
    &send_subflow_premature_end_msg<ntdec_cifs_close_response>);

定义:

存储方法:

void smb1_subdecoder::store_subflow_bunch_on_open(
    handle_t handle, uint64_t subflow_msg_id, CIFS_RESOURCE_TYPE resource_type,
    send_subflow_premature_end_msg_t send_subflow_premature_end_rq_msg,
    send_subflow_premature_end_msg_t send_subflow_premature_end_rsp_msg) { ... }

send_subflow_premature_end_msg_t:

typedef void (smb1_subdecoder::*send_subflow_premature_end_msg_t)
                    (uint64_t, uint8_t, uint64_t, uint8_t);

发送过早结束消息的模板函数(签名不依赖于上述模板参数):

template<class NTDEC_END_OBJECT_T>
void smb1_subdecoder::send_subflow_premature_end_msg(uint64_t subflow_msg_id, uint8_t close_reason,
                                                     uint64_t header_msg_id, uint8_t side)
{
    NTDEC_END_OBJECT_T end_msg;
    _d_smb->proto_fill(end_msg, subflow_msg_id);
    end_msg.header_msg_id = header_msg_id;
    if (_d_smb->transport() == decoder_smb::TRANSPORT::NETBIOS)
    {
        const auto* netbios_msg = _d_smb->get_proto_parent<ntdec_object>();
        if (netbios_msg)
            set_parent_msg_id(end_msg, netbios_msg->msg_id);
    }
    end_msg.side = (side == -1) ? _d_smb->get_last_data_side() : side;

    _d_smb->stream().send_message(end_msg);
}

【问题讨论】:

    标签: c++ templates callback


    【解决方案1】:

    constructing a pointer to member 时,您必须用类名明确限定非静态成员的名称。

    (强调我的)

    1. 如果操作数是非静态成员的限定名,例如 &amp;C::member,结果是指向成员函数的prvalue指针或 指向 C 类中类型 T 的数据成员的指针。请注意,&amp;member C::member 甚至 &amp;(C::member) 也不能用于初始化 指向成员的指针

    例如

    store_subflow_bunch_on_open(
        handle(response_msg.file_id), response_msg.msg_id, 
        static_cast<CIFS_RESOURCE_TYPE>(parm_open2_rsp->resource_type),
        &smb1_subdecoder::send_subflow_premature_end_msg<ntdec_cifs_close_request>,
    //   ^^^^^^^^^^^^^^^^^
        &smb1_subdecoder::send_subflow_premature_end_msg<ntdec_cifs_close_response>);
    //   ^^^^^^^^^^^^^^^^^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      相关资源
      最近更新 更多