c – 重载给我错误:’operator <'不匹配
发布时间:2020-12-16 10:08:25 所属栏目:百科 来源:网络整理
导读:在代码中,我有按waitTime排序的事件队列.我想找到当前时刻应该执行哪些事件,所以我这样做: std::vectorEvent::iterator up = std::upper_bound(queue.begin(),queue.end(),currentTime); 如果我重载 std :: upper_bound将起作用操作符: bool Event::operat
在代码中,我有按waitTime排序的事件队列.我想找到当前时刻应该执行哪些事件,所以我这样做:
std::vector<Event>::iterator up = std::upper_bound(queue.begin(),queue.end(),currentTime); 如果我重载< std :: upper_bound将起作用操作符: bool Event::operator<(const double& currentTime) const { return waitTime < currentTime; } 但是我有一个错误: error: no match for ‘operator<’ (operand types are ‘const double’ and ‘Event’) 我应该如何正确地重载’operator<'? P.S class Event{ public: double startTime; double waitTime; double size; Event(double start,double wait,double size); bool operator<(const Event& otherEvent) const; bool operator<(const double& currentTime) const; bool operator() (const Event & event,const double & right); }; 解决方法
考虑到此错误消息
你需要申报操作符 bool operator<(const double &,const Event &); 似乎在算法中使用了条件 currentTime < *it 另一种方法是调用算法 std::vector<Event>::iterator up = std::upper_bound(queue.begin(),Event { 0.0,currentTime,0.0 }); 这是通过将currentTime转换为Event类型的对象,因为已经重载了运算符<对于Event类型的对象.. bool operator<(const Event& otherEvent) const; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |