2017-12-01 10 views
3

コードではEventsのキューがwaitTimeでソートされています。私はEventsは、現在の瞬間のために実行すべきかを見つけたいので、私はこれを行う:オーバーロードでエラーが発生しました: 'operator <'の一致がありません

std::vector<Event>::iterator up = std::upper_bound(queue.begin(), queue.end(), currentTime); 

私は<演算子オーバーロード場合std::upper_boundは動作します:

bool Event::operator<(const double& currentTime) const 
{ 
    return waitTime < currentTime; 
} 

をしかし、私はエラーがあります。

error: no match for ‘operator<’ (operand types are ‘const double’ and ‘Event’) 

「オペレーター<」を正しくオーバーロードするにはどうすればよいですか?

P.S

class Event{ 
public: 
    double startTime; 
    double waitTime; 
    double size; 

    Event(double start, double wait, double size); 
    bool operator<(const Event& otherEvent) const; 
    bool operator<(const double& currentTime) const; 
    bool operator() (const Event & event, const double & right); 
}; 
+0

endTimeとは何ですか? –

+0

「イベント」は...と定義されていますか? – max66

+3

分割されていないコードの断片ではなく、[mcve]を投稿してください。 –

答えて

3

口座にこのエラーメッセージを撮ります

error: no match for ‘operator<’ (operand types are ‘const double’ and ‘Event’)

あなたは演算子を宣言する必要が

bool operator<(const double &, const Event &); 
アルゴリズムの中に条件

currentTime < *it 

が使用されている別のアプローチは、タイプEventの対象にcurrentTimeのキャストを使用している

std::vector<Event>::iterator up = std::upper_bound(queue.begin(), 
                queue.end(), 
                Event { 0.0, currentTime, 0.0 }); 

のようなアルゴリズムを呼び出すことであると思われる3210

タイプがEventのオブジェクトの場合、既にオーバーロードされている演算子<があるため

bool operator<(const Event& otherEvent) const; 
2
bool Event::operator<(const double& currentTime) const 

満たないのみ次のような状況のためのオペレータ定義:これらの演算子を定義するとき、あなたはそれらの両方を定義する必要があり

bool result = d < e; 

Event e; 
//... 
double d = /*...*/; 
bool result = e < d; 

ませ次のような状況を方法!理想的には非会員機能

bool operator<(const Event& e, const double& currentTime); 
bool operator<(const double& currentTime, const Event& e); 

、それらの両方の定義(なぜ非会員機能を?improve encapsulationに)

ジョンLakosは、彼がまさにこの言うhas a wonderful CPPcon talkを持っています。

関連する問題