C++ std::chrono 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16518705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:26:30  来源:igfitidea点击:

Problems with std::chrono

c++c++11compilationg++chrono

提问by Jehan

I am having trouble compiling with chrono, here is the code:

我在用 chrono 编译时遇到问题,这是代码:

Time.hh

时间.hh

#include        <chrono>

class           Time
{
protected:
  std::chrono::steady_clock::time_point _start_t;
  std::chrono::steady_clock::time_point _now;
  std::chrono::steady_clock::time_point _time;
public:
  Time();
  Time(const Time &other);
  Time          &operator=(const Time &other);
  ~Time();
public:
  void          start();
  double        getDurSec();
  double        getDurMilSec();
private:
  void          setNow();
};

Compilation error:

编译错误:

g++  -W -Wall -Wextra -I./include -std=c++0x  -c -o src/Time/Time.o src/Time/Time.cpp
In file included from src/Time/Time.cpp:11:0:
./include/Time/Time.hh:21:3: error: ‘steady_clock' in namespace ‘std::chrono' does not     name a type
./include/Time/Time.hh:22:3: error: ‘steady_clock' in namespace ‘std::chrono' does not name a type
./include/Time/Time.hh:23:3: error: ‘steady_clock' in namespace ‘std::chrono' does not name a type
src/Time/Time.cpp: In member function ‘void Time::start()':
src/Time/Time.cpp:34:2: error: ‘_time' was not declared in this scope
src/Time/Time.cpp:34:23: error: ‘std::chrono::steady_clock' has not been declared

Etc...

等等...

Tell me if you need more informations.

如果您需要更多信息,请告诉我。

回答by Jehan

You are probably using a g++ version prior to 4.7.0 where std::chrono::steady_clockwas not implemented. If this is the case, you have two solutions:

您可能正在使用std::chrono::steady_clock未实现的4.7.0 之前的 g++ 版本。如果是这种情况,您有两种解决方案:

  • Upgrade your g++ to 4.7.0 or a more recent version.
  • Use instead the old std::chrono::monotonic_clock.
  • 将您的 g++ 升级到 4.7.0 或更新的版本。
  • 改用旧的std::chrono::monotonic_clock.