Sarit8
Sarit8

Reputation: 33

c++ how to call a function which use template

I use a function (calc_ts) which call to another function (send_sequence_to_device) which contain a template.

The decleration of send_sequence_to_device (header):

///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);

The implementation of 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;
}

The implementation of 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;
}

I got the following error in line 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*>&)'|

Upvotes: 0

Views: 38

Answers (3)

WhiZTiM
WhiZTiM

Reputation: 21576

The decleration of send_sequence_to_device (header):

///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);

The implementation of 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 )
{
 .....

Unfortunately, you can't really have separate header and source files for templates... You can though... See: Why can templates only be implemented in the header file?

And your arguments are incomplete for calling the function at the call site in the source calc_ts...

response_t r=send_sequence_to_device(setRegMsg);  // <-- incomplete args

Upvotes: 0

Smeeheey
Smeeheey

Reputation: 10316

The compile error you get is due to you only providing 1 parameter to a function which expects 3 (only 1 of which has a default value).

Upvotes: 1

Tezirg
Tezirg

Reputation: 1639

Your function has 3 parameters, with one optional.

 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);

But your are calling it with one parameter, forgetting the result_list parameter.

You may also encounter problems by defining a template function in seperate header and source files

Upvotes: 0

Related Questions