I'm writing a HeartbeatManager
for my application (in visual studio). The data of 1 heartbeat is stored in a Heart
object. The Heart
objects are stored in an std::set
. For this reason, I am implementing the operator=
, operator<
and operator>
overloads.
When defining these functions you can only use const
members. I tried to do this but still get an error saying I'm not:
passing const HeartbeatManager::Heart' as 'this' argument of 'bool HeartbeatManager::Heart::operator<(const HeartbeatManager::Heart&)'
discards qualifiers [-fpermissive]
Here is the code. I don't see where I'm using a non-constant:
class HeartbeatManager
{
public:
class Heart
{
public:
Heart(const IPAddress _ip, const uint16_t _port, int _lifetime = 5000)
: ip(_ip), port(_port), lifetime(_lifetime) {}
const IPAddress ip;
const uint16_t port;
int lifetime;
/**
* Ages the Heart, returns whether it survived (lifetime after aging > 0)
*/
bool age(int ms)
{
this->lifetime -= ms;
return 0 < this->lifetime;
}
// overloaded operators so heart struct can be sorted and used in map
bool operator=(const Heart& o) {
return ip == o.ip && port == o.port;
}
bool operator<(const Heart& o) {
return (uint32_t)ip < (uint32_t)o.ip || (ip == o.ip && port < o.port);
}
bool operator>(const Heart& o) {
return (uint32_t)ip > (uint32_t)o.ip || (ip == o.ip && port > o.port);
}
};
void heartbeat(IPAddress ip, uint16_t port, int sec = 5000);
};
I'm very new to C++. So if this is a bad practice for making an object fit the set requirements feel free to let me know.
Aucun commentaire:
Enregistrer un commentaire