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

时间:2015-08-19 01:51:55

标签: c++ c++11 g++ chrono

我正在尝试一个简单的程序来打印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

我在做什么事吗?

3 个答案:

答案 0 :(得分:8)

显然,我没有遵循某些最佳做法,只是想让事情适合你

#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

答案 1 :(得分:3)

您应该包含stdint.h个文件。

答案 2 :(得分:0)

如果您确实要添加,请添加“ #define __STDC_LIMIT_MACROS”

参考:https://stackoverflow.com/a/3233069/6728794