c++如何调用一个使用模板的函数
c++ how to call a function which use template
我使用一个函数(calc_ts
),它调用另一个包含模板的函数(send_sequence_to_device
)。
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);
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 )
{
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(源)的实现
///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的函数提供了1个参数(其中只有1个具有默认值)。
您的函数有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参数。
在单独的头文件和源文件
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
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何仅为一个函数添加延迟
- 构造函数正在调用一个使用当前类类型的函数
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 如何创建函数管道,以便函数一个接一个地运行?
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- C :基类调用自己的虚拟函数 - 一个反图案
- 如何在这个交换函数(一个单独的链表)中找到错误
- 两个相同的函数(一个使用模板模式,另一个不使用)
- 你怎么能一次给一个函数一个参数呢
- 为什么要做两个函数?(一个是非const,另一个是const)
- 当代码在其他地方使用时,如何保证函数一个接一个地被调用