C ++中的精度相关错误

时间:2015-08-07 19:45:38

标签: c++ compiler-errors type-conversion

在构建软件时,我收到错误:

../src/internet/model/nampt-socket.cc: In member function ‘ns3::Ptr<ns3::NaMPTSubflow> ns3::NaMPTSocket::SwitchToMultipathMode()’:
../src/internet/model/nampt-socket.cc:881:14: error: cast from ‘ns3::NaMPTSocket*’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive]
    (uint32_t)this,
              ^
Waf: Leaving directory `/home/vikas/ns-allinone-3.14.1/ns-3.14.1/build'
Build failed
 -> task in 'ns3-internet' failed (exit status 1): 
    {task 140476688662992: cxx nampt-socket.cc -> nampt-socket.cc.1.o}
['/usr/bin/g++', '-O0', '-ggdb', '-g3', '-Wall', '-Werror', '-Wno-error=deprecated-declarations', '-fstrict-aliasing', '-Wstrict-aliasing', '-fPIC', '-pthread', '-Ibuild', '-I.', '-DNS3_ASSERT_ENABLE', '-DNS3_LOG_ENABLE', '-DSQLITE3=1', '-DHAVE_IF_TUN_H=1', '-DENABLE_GSL', '../src/internet/model/nampt-socket.cc', '-c', '-o', 'src/internet/model/nampt-socket.cc.1.o']
Traceback (most recent call last):
  File "./build.py", line 214, in <module>
    sys.exit(main(sys.argv))
  File "./build.py", line 205, in main
    build_ns3(config, build_examples, build_tests, args, build_options)
  File "./build.py", line 98, in build_ns3
    run_command([sys.executable, "waf", "build"] + build_options)
  File "/home/vikas/ns-allinone-3.14.1/util.py", line 24, in run_command
    raise CommandError("Command %r exited with code %i" % (argv, retval))
util.CommandError: Command ['/usr/bin/python', 'waf', 'build'] exited with code 1

错误是:

cast from ‘ns3::NaMPTSocket*’ to ‘uint32_t {aka unsigned int}’ loses precision [-fpermissive]

我搜索了错误但是 在我的情况下无法使用uintptr_t 。还有其他解决办法吗?

错误来自的代码是:

Ptr<NaMPTSubflow> 
NaMPTSocket::SwitchToMultipathMode(void)
{
  NS_ASSERT(m_firstSocket != 0);
  NS_ASSERT(m_firstSocket->m_state == SYN_SENT || m_firstSocket->m_state == SYN_RCVD);
  NS_ASSERT(m_firstSocket->m_shellSocket == this);

  // change to NaMPTCC
  // the original socket will be discarded from m_tcp->m_sockets
  m_socketTypeId = NaMPTCC::GetTypeId();
  m_firstSocket = NaMPTSocket::ConvertCongestionControl(m_firstSocket);

  // subflow
  Ptr<NaMPTSubflow> sf = SetupSubflow(m_firstSocket, true);
  sf->m_bBypassSending = true;
  NS_ASSERT(sf->m_baseSocket == m_firstSocket);

  // session name: time-socket-src-des
  if (m_timeTag == 0)
    m_timeTag = Simulator::Now().GetSeconds();
  char buf[128];
  snprintf(buf, 128, "%.6f-%08x-%u.%u.%u.%u->%u.%u.%u.%u", 
            m_timeTag, 
            (uint32_t)this,
            (sf->m_localAddress.Get() >> 24)&0x000000FF,
            (sf->m_localAddress.Get() >> 16)&0x000000FF,
            (sf->m_localAddress.Get() >> 8)&0x000000FF,
            (sf->m_localAddress.Get() )&0x000000FF,
            (sf->m_peerAddress.Get() >> 24)&0x000000FF,
            (sf->m_peerAddress.Get() >> 16)&0x000000FF,
            (sf->m_peerAddress.Get() >> 8)&0x000000FF,
            (sf->m_peerAddress.Get() )&0x000000FF );
  m_sessionName = buf;

  // init connection
  m_localPort = m_firstSocket->m_endPoint->GetLocalPort();
  m_peerPort = m_firstSocket->m_endPoint->GetPeerPort();
  m_nextTx = m_firstSocket->m_nextTxSequence;
  m_nextTx ++; // syn consumes one byte
  m_inputQueue.SetHeadSequence(m_nextTx);

  // next used addr
  AddressSet_t::const_iterator it = m_addresses.find(sf->m_localAddress.Get());
  it++;
  if (it == m_addresses.end() )
    it = m_addresses.begin();
  m_nextAddr = it->first;

  // m_outputQueue is initialized after this function returns.

  m_firstSocket = 0; // this is important...
  return sf;
}

2 个答案:

答案 0 :(得分:2)

我相信correct way to printf a pointer is to use %p。所以你想把代码更改为:

snprintf(buf, 128, "%.6f-%p-%u.%u.%u.%u->%u.%u.%u.%u", 
            m_timeTag, 
            (void *)this,
            (sf->m_localAddress.Get() >> 24)&0x000000FF,
            (sf->m_localAddress.Get() >> 16)&0x000000FF,
            (sf->m_localAddress.Get() >> 8)&0x000000FF,
            (sf->m_localAddress.Get() )&0x000000FF,
            (sf->m_peerAddress.Get() >> 24)&0x000000FF,
            (sf->m_peerAddress.Get() >> 16)&0x000000FF,
            (sf->m_peerAddress.Get() >> 8)&0x000000FF,
            (sf->m_peerAddress.Get() )&0x000000FF );

这避免了转换为任何类型的整数的需要。 %p也以十六进制格式,同时避免在printf语句中指定字段大小。在编译32位或64位时,它会自动调整大小。

答案 1 :(得分:1)

你如何uint32_t而不是uintptr_t?它们应该在同一个标​​题中定义。

但假设你真的没有它,那就自己动手吧。

typedef uint64_t uintptr_t;