error: 'high_resolution_clock' has not been declared

2.7k Views Asked by At

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

2

There are 2 best solutions below

0
Remy Lebeau On BEST ANSWER

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;
0
theebugger On

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;