我在我的项目中使用了SNMP++库,一切工作正常。但是,有一个方法需要在我的.mm文件中获取回调。现在,当我创建一个块并将其作为参数传递给该函数时,它抛出了一个错误:“没有匹配的成员函数用于调用'get_bulk'”。下面是这段代码:
void(^callbackFunc)(int,Snmp*,Pdu&,SnmpTarget&,void*);
callbackFunc = ^(int i,Snmp* s,Pdu& p,SnmpTarget& t,void* v) {
};
snmp.get_bulk(pdu, *target, l_repeaters, l_repetitions,callbackFunc);另外,下面是"get_bulk“函数的函数签名:
int Snmp::get_bulk(Pdu &pdu, // pdu to use
const SnmpTarget &target, // destination target
const int non_repeaters, // number of non repeaters
const int max_reps, // maximum number of repetitions
const snmp_callback callback,// callback to use
const void * callback_data) // callback data
{
pdu.set_type( sNMP_PDU_GETBULK_ASYNC);
return snmp_engine( pdu, non_repeaters, max_reps, target,
callback, callback_data);
}我应该在'callback‘类型中传递什么?
这是SNMP_callback的类型定义:
typedef void (*snmp_callback)(int reason, Snmp *session,
Pdu &pdu, SnmpTarget &target, void *data);在过去的4-5个小时里,我被困在这个问题上,我不知道如何解决这个问题。
发布于 2018-09-20 22:22:15
Apple的代码块不能转换为函数指针,因为它们还包含数据(捕获的变量等)。和引用计数机制。您将需要传递一个自由函数、静态C++类成员函数或一个C++非捕获lambda作为回调。
lambda在语法上最接近于块;然而,只有非捕获的lambda可以转换为函数指针,因此您需要“手动”通过void* callback_data参数传递指向上下文结构或类似结构的指针,该参数可能会作为void* data传递给回调。
lambda看起来像这样:
snmp_callback callback =
[](int reason, Snmp *session, Pdu &pdu, SnmpTarget &target, void *data)
{
// context_struct_type* context = static_cast<context_struct_type*>(data);
};https://stackoverflow.com/questions/52274736
复制相似问题