【问题标题】:error: uint64_t was not declared in this scope when compiling C++ program错误:编译 C++ 程序时未在此范围内声明 uint64_t
【发布时间】:2015-08-19 01:51:55
【问题描述】:

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

我做错了什么吗?

【问题讨论】:

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


    【解决方案1】:

    显然,我并没有遵循某些最佳实践,而只是试图让事情为你工作

    #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
    

    【讨论】:

    • 当我运行它时,我看到了这个错误error: steady_clock has not been declared。当我们已经宣布它时,任何想法为什么会出现这种情况?
    • steady_clock 在 std::chrono 下。使用完全限定名称 (std::chrono::steady_clock) 或通过使用命名空间 std::chrono 使符号可见,如我上面的示例所示
    • 我只尝试了您的示例,然后出现上述错误。我只是照原样使用你的例子。
    • 我正在使用g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3。你认为这在这里很重要吗?
    • 如果我在具有 4.8.2 编译器的 Ubuntu 14 上运行它,那么它工作正常,但如果我在具有 4.6.3 的 Ubuntu 12 上运行它,那么它就不起作用。奇怪吗?
    【解决方案2】:

    您应该包含stdint.h 文件。

    【讨论】:

      【解决方案3】:

      如果你真的想包含,添加“#define __STDC_LIMIT_MACROS”

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-08
        • 1970-01-01
        • 2021-05-13
        • 2013-03-05
        • 2011-01-31
        • 1970-01-01
        • 2018-04-30
        • 1970-01-01
        相关资源
        最近更新 更多