如何解决错误:尚未声明“ high_resolution_clock”
我在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
解决方法
您需要在std::chrono::
,high_resolution_clock
和microseconds
前面指定duration_cast
命名空间限定符,例如:
#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;
,
哦,我刚找到解决方案, 我忘了使用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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。