使用标准::时间::high_resolution_clock时"operator ="不匹配
No match for "operator =" when using std::chrono::high_resolution_clock
当我编译此 timer.hpp 标头文件时,编译器说:
错误:" operanter ="没有匹配(操作数类型为 ‘std :: chrono :: _ v2 :: system_clock :: time_point {aka std :: chrono :: time_point>>}’和 ‘std :: __ success_type>>> :: type {aka std :: chrono ::持续时间>}( end = std :: chrono :: high_resolution_clock :: now(( - start;
我想开始和结束的变量类型是错误的。什么是正确的类型?我想使用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 应该与某些主文件一起使用。
通过在我想测量的一些函数之前调用timer::initTime()
并在功能之后调用timer::endTime()
,我将获得计时结果(此处省略了持续时间的getter(。
此代码有两个问题:
static std::chrono::system_clock::time_point start, end;
/* ... */
void endTime(){
end = std::chrono::high_resolution_clock::now() - start;
}
您将end
声明为时间点,但是在分配操作员的右侧,您要减去两个时间点(now()
和start
(,然后分配给end
。
从逻辑上讲,如果您减去两个时间点,则不会获得新的时间点。例如,如果我想减去"今天的08:15:00" - "今天的08:05:00",将结果描述为" 00:10:00今天"是没有意义的。相反,C Chrono库具有duration
类模板。它旨在表示时间长度(例如两个时间点之间的差异(。
请参阅operator -
Overload编号4:http://en.cppreference.com/w/cpp/chrono/time_point/operator_arith2
我建议观看@howard Hinnant链接到上面的教程视频... Hinnant先生参与了开发std::chrono
和boost::chrono
库。
电位 second 问题是start
具有类型std::chrono::system_clock::time_point
,它可能是与std::chrono::high_resolution_clock::now()
返回的类型不同的类型(不同的时钟((具有std::chrono::high_resolution_clock::time_point
类型(。
- 与'operator='不匹配(操作数类型'String'且"void")
- Valarray 切片数组与 'operator*' 不匹配
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 与"operator<<"不匹配(操作数类型'std::ostream'
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 使用标准::时间::high_resolution_clock时"operator ="不匹配
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- 错误:尝试减去字符串时'operator-'不匹配
- 调用模板方法会导致'operator<'不匹配
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 使用 #omp 时'operator -'不匹配
- 尝试使用 fstream 将字符写入文件:'operator<<'不匹配
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 有没有办法修复此错误:与'operator[]'不匹配(操作数类型为 'empl' 和"int")
- 错误与'operator='不匹配(操作数类型为 'Person' 和"Person*")
- 我得到"将值分配给结构数组 iin 时与'operator='不匹配C++
- 在地图中搜索值时'operator='不匹配
- C++错误:与'operator<<'不匹配