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

前端之家收集整理的这篇文章主要介绍了错误:编译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

猜你在找的C&C++相关文章