Help with a priority queue friend function

I have a priority_queue defined in my Items class as a composition object.

priority_queue <Bid> bidQueue;

Bid is a separate class that deals with the input and output of the bids. I've tried identifying the overloaded < operator as a friend function of the item class and defined it in the item.cpp as:

bool operator<(const Bid &right, const Bid &left)
{
return (right.getBid() < left.getBid());
}

Here it gives an "Error:the object has type qualifiers that are not compatitble with the member function for my left and right" objects.

And I also tried it as a friend function of the Item class but defined in the Bid class:

bool Bid::operator<(const Bid &right, const Bid &left)
{
return (right.bid < left.bid);
}

Here I get "Error: class Bid has no member operator <" and the bid variables are inaccesible. I do have it declared in the bid.h file so I don't know why it's appearing. I am at a loss on how to approach solving this. I'd appreciate any help. Thanks.
It would have been helpful if you had posted your Bid class declaration.

Is Bid::getBid() declared const ?
You can't call a non-const function using a const object.


Thanks for the suggestion. I tried it and it made no difference. Here's the new code.

priority_queue defined in my Items class as a composition object (no change).

priority_queue <Bid> bidQueue;


Declaration in item.h

friend bool Bid::operator<(const Bid &, const Bid &)const;


Decaration in bid.h

bool operator<(const Bid &, const Bid &)const;


Definition in bid.cpp which stil gives the same errors:

bool Bid::operator<(const Bid &right, const Bid &left) const
{
return (right.bid < left.bid);
}

I have <queue> included in all of the files. BTW, the editor is giving me this error even before I do debugging.
If operator< is a member of Bid, then in:

1
2
3
4
5
6
class Bid
{
public:

    bool operator<(const Bid&, const Bid&) const ;
};


operator< is declared to have 3 parameters. The implicit this parameter that all member functions have and the two Bid& parameters explicitly listed in the argument list.

The problem, of course, is that operator< is a binary operator. It may only have two parameters. The solution, should you wish it to remain a member function is to change the declaration and definition so that only one parameter is explicit:

1
2
3
4
5
6
class Bid
{
public:

    bool operator<(const Bid&) const ;
};


Topic archived. No new replies allowed.