不符合'operator>'

时间:2016-05-21 23:04:38

标签: c++ operator-overloading

我在重载那些操作员的次数越少越好。他们自己编译好,但当他们用来定义小于或等于运算符(或大于)时,我得到这个错误:

error: no match for ‘operator>’ (operand types are ‘Job*’ and ‘Job’)
inline bool Job::operator<= (Job rhs) { return !(this > rhs); }

我最初将它们作为operator(const Job& rhs) { ... },但我删除了所有的const和引用语法,看看它是否会起作用,我只是遇到类型问题..但即便就是我得到同样的错误。我做错了什么?

这是代码:

的.cpp:

#include "Job.h"

void Job::setId(int i) { id = i; }
const int Job::processes () const { return process; }
const int Job::ticks() const { return tick; }

inline bool Job::operator< (Job rhs) { return (process * tick) < (rhs.processes() * rhs.ticks()); }
inline bool Job::operator> (Job rhs) { return (process * tick) > (rhs.processes() * rhs.ticks()); }
inline bool Job::operator<= (Job rhs) { return !(this > rhs); }
inline bool Job::operator>= (Job rhs) { return !(this < rhs); }

·H:

#include <string>

class Job {
public:
   Job (std::string desc, int procs, int tcks)
            : description{ desc }, process{ procs }, tick{ tcks }{};
    void setId(int id);
    const int ticks() const;
    const int processes() const;

private:
    std::string description;
const int process;
const int tick;
int id;

bool operator<(Job rhs);
bool operator>(Job rhs);
bool operator<=(Job rhs);
bool operator>=(Job rhs);
};

1 个答案:

答案 0 :(得分:0)

class room { private: event *ev; //here, each room class is given an event class pointer public: void set_bat(); void get_message(); ~room() { delete ev; } // ADDED }; 是指向对象的指针,而不是对象本身。所以,这应该有效:

this

此外,如果某些内容为inline bool Job::operator<= (Job rhs) { return !(*this > rhs); } ,则应将其放入标题中,否则使用您的类的下一个.cpp无法查看实现,因此无法利用内联。