2016-05-17 11 views
-4

"バイナリ '<':タイプ 'const Baloons'(または受け入れ可能な変換はありません)の左辺オペランドを取る演算子が見つかりません"というエラーが発生し、解決策が見つかりません?演算子<優先度キューのエラー

また、私はbaloon.endによって優先順位キューをどのように並べることができますか?

#include <iostream> 
#include <queue> 
#include <vector> 

using namespace std; 

class Baloons 
{ 
public: 
    float start; 
    float end; 

public: 
    Baloons() {} 
    Baloons(float start, float end) : start{ start }, end{ end } {} 
}; 

int main() 
{ 
Baloons baloon1(1, 5); 
Baloons baloon2(4, 7); 
Baloons baloon3(6, 9); 
Baloons baloon4(11, 12); 
std::priority_queue<Baloons> myBaloons; 
myBaloons.push(baloon1); 
myBaloons.push(baloon2); 
myBaloons.push(baloon3); 
myBaloons.push(baloon4); 
while (!myBaloons.empty()) 
{ 
    Baloons b = myBaloons.top(); 
    cout << b.start << " -> " << b.end << " "; 
    myBaloons.pop(); 
} 
system("pause"); 
return 0; 

}

+0

にそれを与えます具体的な結果はこちらhttp://stackoverflow.com/search?q=%22binary+%27%3C%27%3A+no+ope標準的なコレクションとアルゴリズムで '<'がどれくらい使われているかを考慮して、rator + found + which +は+ a +左辺+オペランド+ +型%22を取る。 – vu1p3n0x

答えて

1

は、次の例のように、あなたの風船クラスにoperator<を追加する必要があります。

bool operator<(const Baloons& rhs) const { 
    return std::make_pair(start,end) < std::make_pair(rhs.start,rhs.end); 
} 

あなたのコレクション内の要素を順序付けするために使用される

+0

私を助けてくれてありがとう。宿題としての問題 –

0

プライオリティキューは、コンテナを並べ替えられていますが、コンパイラはBaloonsをソートする方法を知りません。コンテナをソートできるように、カスタムoperator<を定義する必要があります。

これは明らかに宿題の問題です(これは 'myBaloons.pop()'というジョークのためだけに作成されたものです)ので、私は全体を遠ざけたくありません。あなたのバルーンクラス。

bool operator<(const Baloons& rhs) const { 
    // return true if this Baloons is less than 'rhs' 
} 

あなたのバルーンを比較することができます。 (例:if (baloon1 < baloon2) { /* do stuff */ })優先キューでは、新しいオブジェクトを挿入するときにこの比較が行われます。

0

あなたはBaloonsのためにそれ以外の場合は、オーバーロードoperator<を提供する必要がありますコンパイラはstd::priority_queue<Baloons>の要素をどのように注文するかを知らない。

bool operator<(const Baloons& _other) const { 
    return end < _other.end; 
} 

または何でもあなたはその場で、ここでreturn文の比較したい:たとえば、あなたのBaloonsクラスでこのような何かを定義します。

0

は、

bool operator < (const Baloons& lhs, const Baloons& rhs) 
{ 
    return lhs.end < rhs.end; 
} 

bool operator < (const Baloons&, const Baloons&)提供またはファンクタCustomComparerを作成し、私は得ることはありません実際には驚いているstd::priority_queue<Ballons, std::vector<Ballons>, CustomComparer>

struct CustomComparer 
{ 
    bool operator() (const Baloons& lhs, const Baloons& rhs) const 
    { 
     return lhs.end < rhs.end; 
    } 
}; 

以降

std::priority_queue<Ballons, std::vector<Ballons>, CustomComparer> myBaloons;