c – 重载给我错误:’operator <'不匹配

c – 重载给我错误:’operator <'不匹配,第1张

概述在代码中,我有按waitTime排序的事件队列.我想找到当前时刻应该执行哪些事件,所以我这样做: std::vector<Event>::iterator up = std::upper_bound(queue.begin(), queue.end(), currentTime); 如果我重载< std :: upper_bound将起作用 *** 作符: bool Event::operator<(co 在代码中,我有按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);};
解决方法 考虑到此错误消息

error: no match for ‘operator<’ (operand types are ‘const double’ and
‘Event’)

你需要申报 *** 作符

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;
总结

以上是内存溢出为你收集整理的c – 重载给我错误:’operator <'不匹配全部内容,希望文章能够帮你解决c – 重载给我错误:’operator <'不匹配所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1217898.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存