0

我在使用 NS-3 API 的某些部分时遇到了一个奇怪的错误。这是我的错误信息:

error: passing ‘const ns3::TopologyReader::Link’ as ‘this’ argument of ‘std::string ns3::TopologyReader::Link::GetAttribute(std::string)’ discards qualifiers

这是导致问题的代码:

TopologyReader::ConstLinksIterator iter;
int num = 0;
for (iter = topologyReader->LinksBegin (); iter != topologyReader->LinksEnd(); iter++, num++)
  {
    std::istringstream fromName(iter->GetFromNodeName ());
    std::istringstream toName (iter->GetToNodeName ());
    iter->GetToNodeName();
    std::string w = "Weight";
    std::string weightAttr = (iter)->GetAttribute(w); // <- error
    /* snip */
  }

根据TopologyReader::Link 的文档,我认为这可能与它GetAttribute(std::string)不是函数有关,而其他函数则被声明为函数。但是,我不确定如何解决此问题。constGetFromNodeName(void)GetToNodeName(void)const

编辑:函数签名如图所示(来自链接的文档):

std::string ns3::TopologyReader::Link::GetFromNodeName (void) const
std::string ns3::TopologyReader::Link::GetToNodeName (void) const
std::string ns3::TopologyReader::Link::GetAttribute (std::string name)  
4

1 回答 1

1

你的分析是正确的。显而易见的解决方法是使其GetAttribute成为一个 const 函数。它的名字表明它应该是 const。不过,您可能无权更改该代码。

另一种方法是找到某种方法来获取非常量对象来调用函数。也许您可以声明iter为 aLinksIterator而不是 a ConstLinksIterator

作为最后的手段,您可以尝试使用const_cast告诉编译器在所谓的 const 对象上调用非常量方法真的很安全。

于 2012-03-02T02:10:36.543 回答