-3

I am facing "binary '<': no operator found which takes a left-hand operand of type 'const Baloons' (or there is no acceptable conversion)" error and I cannot find any solution?

I would also like to ask how can I order the priority queue by 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;

}

  • I'm actually surprised I don't get a specific result here http://stackoverflow.com/search?q=%22binary+%27%3C%27%3A+no+operator+found+which+takes+a+left-hand+operand+of+type%22 considering how much `<` is used in standard collections and algorithms... – kmdreko May 17 '16 at 21:44

4 Answers4

1

You need to add a operator< to you ballons class, for example:

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

it will be used to order elements in your collection

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

Priority queues are sorted containers, but the compiler doesn't know how to sort Baloons. You have to define a custom operator< so your container can be sorted.

This is clearly a homework problem (pretty sure this was created just for the 'myBaloons.pop()' joke) so I don't want to give the whole thing away... Add something like this to your Baloons class.

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

That way your Baloons can be compared. (e.g. if (baloon1 < baloon2) { /* do stuff */ }) The priority queue does this comparison behind the scenes as you insert new objects.

0x5453
  • 12,753
  • 1
  • 32
  • 61
0

You need to provide an overloaded operator< for Baloons otherwise the compiler doesn't know how to order the elements of the std::priority_queue<Baloons>. For example, define something like this in your Baloons class:

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

Or whatever you want to compare in-place of the return statement here.

sjrowlinson
  • 3,297
  • 1
  • 18
  • 35
0

Provide bool operator < (const Baloons&, const Baloons&),

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

or create a functor CustomComparer and give it to std::priority_queue<Ballons, std::vector<Ballons>, CustomComparer>

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

and later

std::priority_queue<Ballons, std::vector<Ballons>, CustomComparer> myBaloons;
Jarod42
  • 203,559
  • 14
  • 181
  • 302