'sTD :: String getAttribute (std :: string)'의 'this'인수로 'const 링크'를 전달합니다.

StackOverflow https://stackoverflow.com/questions/9527003

  •  15-11-2019
  •  | 
  •  

문제

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 */
  }
.

토폴로지 리드 인 :: 링크 설명서 : 다른 기능, GetAttribute(std::string)constGetFromNodeName(void) 함수로 선언됩니다.그러나 나는이 문제를 해결하는 방법을 모르겠습니다.

편집 : 함수 서명은 (링크 된 문서에서) 그림과 같습니다.

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)  
.

도움이 되었습니까?

해결책

Your analysis is correct. The obvious fix is to make GetAttribute be a const function. Its name suggests it should be const. It might not be in your power to change that code, though.

The alternative is to find some way of getting a non-const object to call the function on. Maybe you could declare iter as a LinksIterator instead of a ConstLinksIterator.

As a last resort, you could try using const_cast to tell the compiler that it's really safe to call a non-const method on a supposedly const object.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top