【问题标题】:error: cannot take the address of an rvalue of type 'void'. Two classes with same workflow but one throws error错误:无法获取“void”类型的右值的地址。两个具有相同工作流程的类,但一个会引发错误
【发布时间】:2020-03-02 12:05:54
【问题描述】:

我有两个具有相似工作流程的函数。一个是来自 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++ 新手。

【问题讨论】:

  • 此问题中显示的代码无法满足 stackoverflow.com 对minimal reproducible example 的要求,因此 stackoverflow.com 上的任何人都不太可能确定问题所在。这个问题必须是edited 以显示一个最小示例,不超过一两页代码(“最小”部分),任何人都可以剪切/粘贴、编译、运行和重现所描述的问题(“ reproducible”部分)完全如图所示(这包括任何辅助信息,例如程序的输入)。请参阅How to Ask 了解更多信息。
  • 你真的想要返回值的地址(这通常是个坏主意)还是想要传递一个函数指针?
  • 如果您查看工作中的消费者代码,您会注意到它没有调用函数。
  • 你为什么将 ProactiveProducer::m_prefix 传递给 ProactiveProducer::SendData ?它已经是一个类成员,它可以访问m_prefix。如果您想保留原始签名以供其他用途,可以添加重载 void SendData() { return SendData(prefix); }

标签: c++ networking memory-address void-pointers ns-3


【解决方案1】:

错误信息

错误:无法获取 '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);

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

【讨论】:

    【解决方案2】:

    改变这一行

    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

    【讨论】:

    • 我的意思是,第二个参数实际上是一个没有任何参数的成员函数的地址。因此,ProactiveProducer::SendData 方法的参数应该作为 Simulator::Schedule 方法的参数传递。 nsnam.org/doxygen/…
    • 我不是专家。但它看起来像 doxygen 中的情况。
    • 您是对的,但可能会添加一个指向引用的链接以表明此重载确实存在。它有助于避免一些混乱;)
    • 我现在明白了,非常感谢。我没有意识到我正在传递一个成员函数作为参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 2020-09-01
    • 1970-01-01
    • 2021-11-14
    • 2020-04-24
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多