【问题标题】:error: 'high_resolution_clock' has not been declared错误:“high_resolution_clock”尚未声明
【发布时间】:2020-08-22 06:28:32
【问题描述】:

我在 Windows 10 上使用 g++ 8.1.0 版,但当我尝试编译时仍然

auto start=high_resolution_clock::now();
rd(n);
auto stop=high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop-start);
cout<<duration.count()<<endl;

我得到的错误是

error: 'high_resolution_clock' has not been declared
 auto start=high_resolution_clock::now();
            ^~~~~~~~~~~~~~~~~~~~~

我已经包含了 chrono 和 time.h

【问题讨论】:

  • high_resolution_clock --> std::chrono::high_resolution_clock

标签: c++ time chrono high-resolution-clock


【解决方案1】:

您需要在high_resolution_clockmicrosecondsduration_cast前面指定std::chrono::命名空间限定符,例如:

#include <chrono>

auto start = std::chrono::high_resolution_clock::now();
rd(n);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop-start);
std::cout << duration.count() << std::endl;

否则,您可以使用using 语句代替,例如:

#include <chrono>
using namespace std::chrono;

auto start = high_resolution_clock::now();
rd(n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop-start);
std::cout << duration.count() << std::endl;

或:

#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::microseconds;
using std::chrono::duration_cast;

auto start = high_resolution_clock::now();
rd(n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop-start);
std::cout << duration.count() << std::endl;

【讨论】:

    【解决方案2】:

    哦,我刚刚得到了解决方案, 我忘了使用 chrono 命名空间 所以代码应该是:

    auto start=chrono::high_resolution_clock::now();
    rd(n);
    auto stop=chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::microseconds>(stop-start);
    cout<<duration.count()<<endl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 2012-11-24
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多