相关疑难解决方法(0)

c ++错误C2662无法将'this'指针从'const Type'转换为'Type&'

我试图重载c ++运算符==但我得到一些错误...

错误C2662:'CombatEvent :: getType':无法将'this'指针从'const CombatEvent'转换为'CombatEvent&'

这个错误就在这一行

if (lhs.getType() == rhs.getType())
Run Code Online (Sandbox Code Playgroud)

看下面的代码:

class CombatEvent {

public:
    CombatEvent(void);
    ~CombatEvent(void);

    enum CombatEventType {
        AttackingType,
        ...
        LowResourcesType
    };

    CombatEventType getType();
    BaseAgent* getAgent();

    friend bool operator<(const CombatEvent& lhs, const CombatEvent& rhs) {

        if (lhs.getType() == rhs.getType())
            return true;

        return false;
    }

    friend bool operator==(const CombatEvent& lhs, const CombatEvent& rhs) {

        if (lhs.getType() == rhs.getType())
            return true;

        return false;
    }

private: 
    UnitType unitType;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮忙吗?

c++ operator-overloading syntax-error friend-function

23
推荐指数
3
解决办法
4万
查看次数