简体   繁体   中英

error: 'high_resolution_clock' has not been declared

I am using g++ version 8.1.0 on windows 10 but still when I try to compile

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;

I get the error as

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

I have included both chrono and time.h

You need to specify the std::chrono:: namespace qualifier in front of high_resolution_clock , microseconds , and duration_cast , eg:

#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;

Otherwise, you can use using statements instead, eg:

#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;

or:

#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;

Oh, I just got the solution, I forgot to use the chrono namespace so the code should be:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM