传递我的参考 c++ 时使用类的公共函数时出错

Error in using class's public function when passed my reference c++

本文关键字:出错 函数 我的 参考 c++      更新时间:2023-10-16

我试图为向量创建一个排序函数。这是我写的

struct Xgreater
    {
        bool operator()( const lineCommand& lx, const lineCommand& rx ) const {
            return lx.getEndTime() < rx.getEndTime();
        }
    };

我的班级在哪里:

    class lineCommand {
    public:
        lineCommand(float startTime, float endTime);
        virtual ~lineCommand();
    //those are short inline functions:
    //setting the starting time of the command
    void setStartTime(const float num){mStartTime=num;};
    //setting the ending time of the command
    void setEndTime(const float num){mEndTime=num;};
    // returning the starting time of the command
    float getStartTime(){return mStartTime;};
    // returning the ending time of the command
    float getEndTime(){return mEndTime;};
private:
    float mStartTime;
    float mEndTime;
};

不在xgreater中。我在eclipse中得到错误提示:

Invalid arguments '
Candidates are:
float getEndTime()
在:

lx.getEndTime and rx.getEndTime

按如下方式声明函数

float getEndTime() const {return mEndTime;};
                   ^^^^^

在这个操作符声明中

    bool operator()( const lineCommand& lx, const lineCommand& rx ) const {
        return lx.getEndTime() < rx.getEndTime();
    }

参数lxrx为常量引用。因此,只能使用这些引用调用带有限定符const的成员函数。

与声明函数getStartTime

相同
float getStartTime() const {return mStartTime;};