std :: chrono的问题

9 c++ compilation g++ c++11 c++-chrono

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

Time.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();
};
Run Code Online (Sandbox Code Playgroud)

编译错误:

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
Run Code Online (Sandbox Code Playgroud)

等等...

告诉我你是否需要更多信息.

Jeh*_*han 13

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

  • 将g ++升级到4.7.0或更新版本.
  • 使用旧的std::chrono::monotonic_clock.

  • @Mayerz - `monotonic_clock`是`chrono`规范的早期草稿.它被`steady_clock`取代,它具有更多有用的语义. (4认同)
  • @Mayerz认为自己很幸运.我的学校仍然使用g ++ 3.4 :) (3认同)