Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:将x作为x的“this”参数传递将丢弃限定符_C++_Class_C++11_Operator Overloading_Stdset - Fatal编程技术网

C++ 错误:将x作为x的“this”参数传递将丢弃限定符

C++ 错误:将x作为x的“this”参数传递将丢弃限定符,c++,class,c++11,operator-overloading,stdset,C++,Class,C++11,Operator Overloading,Stdset,我正在为我在visual studio中的应用程序编写一个HeartbeatManager。1次心跳的数据存储在一个心脏对象中。心脏对象存储在std::集合中。出于这个原因,我正在实现操作符=,操作符重载 定义这些函数时,只能使用常量成员。我尝试过这样做,但仍然得到一个错误,说我不是: passing const HeartbeatManager::Heart' as 'this' argument of 'bool HeartbeatManager::Heart::operator<(c

我正在为我在visual studio中的应用程序编写一个HeartbeatManager。1次心跳的数据存储在一个心脏对象中。心脏对象存储在std::集合中。出于这个原因,我正在实现操作符=,操作符重载

定义这些函数时,只能使用常量成员。我尝试过这样做,但仍然得到一个错误,说我不是:

passing const HeartbeatManager::Heart' as 'this' argument of 'bool HeartbeatManager::Heart::operator<(const HeartbeatManager::Heart&)' 
discards qualifiers [-fpermissive]
这是代码。我看不出我在哪里使用了非常量:

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);
};

我对C++很陌生。因此,如果这是一种使对象符合设置要求的错误做法,请随时通知我。

如果希望允许对常量对象调用比较运算符,请替换

bool operator < (const Heart &o) {

等等

还请注意,您已经实现了operator=,而不是operator=

您正在对心脏进行const限定的左值引用。表示函数未对传递的实例进行变异。因此,它们都应该是

心脏对象存储在std::集合中。因此,我正在实现=、<和>运算符


定义尽可能多的操作符是很好的。更多的操作符确实给了类更大的灵活性。但是,使用std::set仅对运算符std::set仅对运算符进行对正operator@SidS如果您指定了一个比较器,那么您就可以不使用已定义的运算符。因此,一个集合证明了运算符返回的正确性*这。运算符>可以简单地返回o<*此值;
bool operator < (const Heart &o) const {
bool operator==(const Heart& o) const
//          ^^^--> typo         ^^^^^
{
    return ip == o.ip && port == o.port;
}
bool operator<(const Heart& o) const
//                             ^^^^^
{
    return (uint32_t)ip < (uint32_t)o.ip || (ip == o.ip && port < o.port);
}
bool operator>(const Heart& o)  const
//                              ^^^^^
{
    return (uint32_t)ip > (uint32_t)o.ip || (ip == o.ip && port > o.port);
}
bool operator< (const Heart& o) const
{
    return std::tie(ip, port) < std::tie(o.ip, o.port);
}
bool operator> (const Heart& o)  const
{
    return std::tie(o.ip, o.port) < std::tie(ip, port);
}
bool HeartbeatManager::Heart::operator<(const HeartbeatManager::Heart&) const