gpt4 book ai didi

c++ - 传递我的引用 C++ 时使用类的公共(public)函数时出错

转载 作者:行者123 更新时间:2023-11-30 01:47:11 25 4
gpt4 key购买 nike

我正在尝试为 vector 创建一个排序函数。这是我写的

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

关于c++ - 传递我的引用 C++ 时使用类的公共(public)函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com