c ++如何调用使用模板的函数

时间:2016-05-04 12:13:18

标签: c++ templates

我使用一个函数(calc_ts)来调用包含模板的另一个函数(send_sequence_to_device)。

send_sequence_to_device(标题)的删除:

///Send sequence to list of devices
template<class T>
    response_t send_sequence_to_device(std::map<const string_t, T*> msg2device_p,std::vector<response_t>& result_list, ushort num_attempts=SEND_NUM_ATTEMPTS);

send_sequence_to_device(source)的实现:

template<class T>
response_t send_sequence_to_device( std::map<const string_t,T*> msg2device_p, std::vector<response_t>& result_list, ushort num_attempts )
{
    bool is_ok_flag = true;
    response_t r;
    raftor1_logs_t* rlogs;
    typename std::map<const string_t, T*>::iterator msg_it;
    for( msg_it=msg2device_p.begin(); msg_it!=msg2device_p.end() and is_ok_flag; msg_it++ )
    {
        r = msg_it->second->send(msg_it->first, true, num_attempts);
        result_list.push_back(r);
        is_ok_flag = is_ok_flag and is_ok(r);

        if( not(is_ok_flag) )
        {
            stringstream ss;
            ss << "ALERT: Sequence aborted due to error on message [" << msg_it->first << "] ";
            if( r.erred() )
                ss << "due to communication failure.";
            else
                ss << "with error message [" << r.msg << "].";
            rlogs->alert.begin_record();
            rlogs->alert.write( ss.str() );
            rlogs->alert.end_record();
        }
    }

    if( is_ok_flag )
        r.set_ok("ok.\n");

    return r;
}

calc_ts(source)的实现

///Calculate of the time slot
bool link_layer_manager_t::calc_ts()
{
    std::map<const string_t, lm_device_t *>setRegMsg={};

    if (frac_msb>51200 and frac_msb<51968)
    {
       setRegMsg={{"trx_set_jr_fraction ' + frac_msb +' ' + frac_lsb +'", &rx}};
       response_t r=send_sequence_to_device(setRegMsg);
       return True;
    }
    else
       return False;
}

我在第response_t r=send_sequence_to_device(setRegMsg);行中遇到以下错误:

error: no matching function for call to 'send_sequence_to_device(std::map<const std::basic_string<char>, lm_device_t*>&)'|

3 个答案:

答案 0 :(得分:1)

您得到的编译错误是由于您只为需要3的函数提供了1个参数(其中只有1个具有默认值)。

答案 1 :(得分:0)

  

send_sequence_to_device (标题)的删除

///Send sequence to list of devices
template<class T>
response_t send_sequence_to_device(std::map<const string_t, T*> msg2device_p,std::vector<response_t>& result_list, ushort
num_attempts=SEND_NUM_ATTEMPTS);
     

send_sequence_to_device的实施(来源)

template<class T>
response_t send_sequence_to_device( std::map<const string_t,T*> msg2device_p, std::vector<response_t>& result_list, ushort
num_attempts )
{
 .....

不幸的是,

在源calc_ts ...

中调用呼叫站点的函数时,您的参数不完整
response_t r=send_sequence_to_device(setRegMsg);  // <-- incomplete args

答案 2 :(得分:0)

你的功能有3个参数,一个是可选的。

 response_t send_sequence_to_device(std::map<const string_t, T*> msg2device_p,std::vector<response_t>& result_list, ushort num_attempts=SEND_NUM_ATTEMPTS);

但是你用一个参数调用它,忘记了result_list参数。

通过在单独的标题和源文件中定义模板函数,您可能也会遇到问题