错误:无法获取类型为"void"的右值的地址。具有相同工作流的两个类,但其中一个类引发错误



我有两个具有类似工作流的函数。一个是NS3的修改示例,另一个是生产者节点的修改自旋。

我的生产者

void
ProactiveProducer::SendData(Name dataName)
{
// dataName.append(m_postfix);
// dataName.appendVersion();
if (!m_active)
return;
NS_LOG_FUNCTION_NOARGS();
auto data = make_shared<Data>();
data->setName(dataName);
data->setFreshnessPeriod(::ndn::time::milliseconds(m_freshness.GetMilliSeconds()));
data->setContent(make_shared< ::ndn::Buffer>(m_virtualPayloadSize));
Signature signature;
SignatureInfo signatureInfo(static_cast< ::ndn::tlv::SignatureTypeValue>(255));
if (m_keyLocator.size() > 0) {
signatureInfo.setKeyLocator(m_keyLocator);
}
signature.setInfo(signatureInfo);
signature.setValue(::ndn::makeNonNegativeIntegerBlock(::ndn::tlv::SignatureValue, m_signature));
data->setSignature(signature);
NS_LOG_INFO("node(" << GetNode()->GetId() << ") responding with Data: " << data->getName());
// to create real wire encoding
data->wireEncode();
m_transmittedDatas(data, this, m_face);
m_appLink->onReceiveData(*data); 
ScheduleNextPacket();
}
void
ProactiveProducer::ScheduleNextPacket()
{
NS_LOG_DEBUG ("m_sendEvent: " << m_sendEvent.IsRunning());
if (m_firstTime) {
m_sendEvent = Simulator::Schedule(Seconds(0.0), &ProactiveProducer::SendData(m_prefix), this);
m_firstTime = false;
} else if (!m_sendEvent.IsRunning()) {
m_sendEvent = Simulator::Schedule(Seconds(1.0 / m_frequency), &ProactiveProducer::SendData(m_prefix), this);
}
}

NS3消费者

void
ModConsumer::SendPacket()
{
// if the application isn't running don't do anything
if (!m_active)
return;
NS_LOG_FUNCTION_NOARGS();
// Will be an invalid packet
uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
/*
if the integer is positice the loop runs infinitely --> poor code
- when the size of the list goes to 0 it will exit and therefore seq will be max int
- the consumer seems to work based off of packets to be retransmitted.
- removes the first entry in list of packets to be retransmitted
- removes packet from list of retransmissions
- then transmits that?
*/
while (m_retxSeqs.size()) {
seq = *m_retxSeqs.begin();
m_retxSeqs.erase(m_retxSeqs.begin());
break;
}
// will check fail conditions or increment sequence
if (seq == std::numeric_limits<uint32_t>::max()) {
// NS_LOG_DEBUG ("Reached max Sequence: " << seq << " max_seq: " << m_seq);
if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
if (m_seq >= m_seqMax) {
NS_LOG_DEBUG ("maximum sequence number has been requested, m_seq: " << m_seq << " m_seqMax: " << m_seqMax);
return; // we are totally done
}
}
seq = m_seq++;
}
shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName);
nameWithSequence->appendSequenceNumber(seq);
shared_ptr<Interest> interest = make_shared<Interest>();
interest->setNonce(m_rand->GetValue(0, std::numeric_limits<uint32_t>::max()));
interest->setName(*nameWithSequence);
interest->setCanBePrefix(false);
time::milliseconds interestLifeTime(m_interestLifeTime.GetMilliSeconds());
interest->setInterestLifetime(interestLifeTime);
interest->setMustBeFresh(true);
// NS_LOG_DEBUG ("Requesting Interest: n" << *interest);
// NS_LOG_INFO("> Interest for " << seq);
WillSendOutInterest(seq);
m_transmittedInterests(interest, this, m_face);
m_appLink->onReceiveInterest(*interest);
ScheduleNextPacket();
}
// Yes this is a different class, pasting for convenience
void
ModConsumerCbr::ScheduleNextPacket()
{
NS_LOG_DEBUG ("m_sendEvent: " << m_sendEvent.IsRunning());
if (m_firstTime) {
m_sendEvent = Simulator::Schedule(Seconds(0.0), &ModConsumer::SendPacket, this);
m_firstTime = false;
} else if (!m_sendEvent.IsRunning()) {
m_sendEvent = Simulator::Schedule(Seconds(1.0 / m_frequency), &ModConsumer::SendPacket, this);
}
}

当我运行代码时,编译器为生产者类抛出以下错误

error: cannot take the address of an rvalue of type 'void'
m_sendEvent = Simulator::Schedule(Seconds(0.0), &ProactiveProducer::SendData(m_prefix), this);

我想我理解为什么会抛出错误。我正在传递一个void类型的函数。它不应该是可寻址的。因此,我无法理解消费者是如何正确编译和运行的。对于我遗漏的任何建议,我们将不胜感激。我是C++新手。

错误消息

错误:无法获取类型为"void"的右值的地址

足够清楚

在本声明中,

m_sendEvent = Simulator::Schedule(Seconds(0.0), &ProactiveProducer::SendData(m_prefix), this);

第二个参数是一个函数调用表达式,其类型为void,即函数SendData的返回类型。因此,完全不清楚你试图实现的是什么,获取一个类型为void的不完整对象的ab地址,而且它是一个右值。

与此声明中的代码相反

m_sendEvent = Simulator::Schedule(Seconds(0.0), &ModConsumer::SendPacket, this);

获取成员函数的地址。所以这些调用在语义上是不同的。

更改此行

m_sendEvent = Simulator::Schedule(Seconds(0.0), &ProactiveProducer::SendData(m_prefix), this);

到这个

m_sendEvent = Simulator::Schedule(Seconds(0.0), &ProactiveProducer::SendData, this, m_prefix);

参考:https://www.nsnam.org/doxygen/classns3_1_1_simulator.html#aec5dd434c42edd6c38ef249d2960c321

最新更新