微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误:编译C程序时未在此作用域中声明uint64_t

我正在尝试一个简单的程序来打印steady_clock的时间戳值,如下所示:
#include <iostream>
#include <chrono>
using namespace std;
int main ()
{
  cout << "Hello World! ";
  uint64_t Now = duration_cast<milliseconds>(steady_clock::Now().time_since_epoch()).count();
  cout<<"Value: " << Now << endl;

  return 0;
}

但每当我像这样g -o abc abc.cpp一样编译时,我总是收到一个错误

In file included from /usr/include/c++/4.6/chrono:35:0,from abc.cpp:2:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard,C++0x. This support is currently experimental,and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
abc.cpp: In function âint main()â:
abc.cpp:7:3: error: âuint64_tâ was not declared in this scope
abc.cpp:7:12: error: expected â;â before âNowâ
abc.cpp:8:22: error: âNowâ was not declared in this scope

我在做什么事吗?

解决方法

显然,我并没有遵循某些最佳实践,只是试图让事情适合你
#include <iostream>
#include <chrono>
#include <cstdint> // include this header for uint64_t

using namespace std;
int main ()
{
  {
    using namespace std::chrono; // make symbols under std::chrono visible inside this code block
    cout << "Hello World! ";
    uint64_t Now = duration_cast<milliseconds>(steady_clock::Now().time_since_epoch()).count();
    cout<<"Value: " << Now << endl;
  }

  return 0;
}

然后使用C 11启用编译(在您的情况下为c 0x)

g++ -std=c++0x -o abc abc.cpp

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐