在我的代码中,我调用这样的函数:
Simulator::Schedule (Seconds(seconds),
&HelloProtocol::sendScheduledInterest(seconds), this, seconds);
Run Code Online (Sandbox Code Playgroud)
这是上述功能的签名:
/**
* @param time the relative expiration time of the event.
* @param mem_ptr member method pointer to invoke
* @param obj the object on which to invoke the member method
* @param a1 the first argument to pass to the invoked method
* @returns an id for the scheduled event.
*/
template <typename MEM, typename OBJ, typename T1>
static EventId Schedule (Time const &time, MEM mem_ptr, OBJ obj, T1 a1);
Run Code Online (Sandbox Code Playgroud)
函数sendScheduledInterest()的定义是:
void
HelloProtocol::sendScheduledInterest(uint32_t seconds)
{
//...
}
Run Code Online (Sandbox Code Playgroud)
我收到以下编译错误:
hello-protocol.cpp: In member function ‘void ns3::nlsr::HelloProtocol::scheduleInterest(uint32_t)’:
hello-protocol.cpp:58:60: error: lvalue required as unary ‘&’ operand
Run Code Online (Sandbox Code Playgroud)
如果我&在函数调用之前删除它,它会给出以下错误:
hello-protocol.cpp: In member function ‘void ns3::nlsr::HelloProtocol::scheduleInterest(uint32_t)’:
hello-protocol.cpp:58:75: error: invalid use of void expression
Run Code Online (Sandbox Code Playgroud)
HelloProtocol::sendScheduledInterest是一个void功能.这意味着它不返回任何值.你既不能在void函数的返回值上调用operator(&)的地址,也不能将它作为参数传递给另一个函数,除非该类型也是void,这只有在涉及某些模板时才会发生.
看来你实际上打算将函数指针作为参数传递,如下所示:
Simulator::Schedule(
Seconds(seconds),
&HelloProtocol::sendScheduledInterest,
this,
seconds);
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,编译器都会告诉您具体问题.
在第一种情况下,void表达式不是左值.您可以将左值视为可以在赋值语句的左侧分配的值.运算符(&)的地址只能应用于左值.
在第二种情况下,您尝试使用不允许的void表达式,即作为其形式参数类型为非void的函数的参数.
| 归档时间: |
|
| 查看次数: |
1714 次 |
| 最近记录: |