When I compiled this timer.hpp header file below, the compiler said:
error: no match for ‘operator=’ (operand types are ‘std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point > >}’ and ‘std::__success_type > >::type {aka std::chrono::duration >}’) end = std::chrono::high_resolution_clock::now() - start;
I guess the variable type for start and end is wrong. What is the correct type? I want to use std::chrono::high_resolution_clock.
#include <chrono> namespace timer{ static std::chrono::system_clock::time_point start, end; void initTime(){ start = std::chrono::high_resolution_clock::now(); } void endTime(){ end = std::chrono::high_resolution_clock::now() - start; } } timer.hpp is supposed to be used with some main file.
By calling timer::initTime() before some function that I want to measure and calling timer::endTime() after the function, I would get the timing result (the getter for the duration time is omitted here).
chrono: youtube.com/watch?v=P32hvk8b13M It addresses issues such as these.