• Advertisement
Sign in to follow this  

Over ridden comparison operator returning false comparisons

This topic is 1372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yet another issue I'm having when over riding operators. At the moment I'm trying to figure out why when I over ride the "<" operator it consitantly gives false comparisons for certain pieces of input. I currently have an enum type called Rank as shown.

enum Rank
{
    TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
};

These are given the value 0-12 respectively. In the implentation class I am just trying to do this:

bool operator<(Card &lhs, Card &rhs)
{
    return lhs.getRank() < rhs.getRank();
}

For the most part it works, however every time I run the program it gives me a warning that says:

"warning: comparison between signed and unsigned integer expressions [-Wsign-compare]"

 

It will then evaluate most of the comparison correctly, however it will always place the KING and the ACE ranks between EIGHT and SEVEN when ordering the ranks from strongest to weakest. Here is some sample results with their rankings next to them.

 

1.) Queen(10), Jack(9), King(11), Six(4), Seven(5)

2.) Ten(8), Eight(6), Ace(12), Six(4), Two(0)

3.) Queen(10), Nine(7), Ace(12), King(11), Seven(5)

 

When I compare the ranks without using the over ridden comparison, but rather by doing it the normal way all the cards will compare as they should and go into order. I've been scratching my head at this for a few hours so if you guys know what's up that'd be great.

 

I can post my code if you feel like reading it, but I don't think the cause of the problem is in something I haven't shown.

Share this post


Link to post
Share on other sites
Advertisement
It would be helpful if you could write a minimal program that shows the problem. By "minimal" I mean that if you remove anything from it, it won't show the problem.

In the process of creating such a program you might find out yourself what the problem is. If you still can't figure it out, you have a nice piece of code to post here so we can reproduce the problem.

Share this post


Link to post
Share on other sites


When I compare the ranks without using the over ridden comparison, but rather by doing it the normal way all the cards will compare as they should and go into order. I've been scratching my head at this for a few hours so if you guys know what's up that'd be great.

 

How have you tried to debug this? When you put a breakpoint inside of the comparison operator, what happens inside of there?

 

Also, for your compile warning, what is the line number? What does that code look like?

 

I don't think you're actually comparing your cards. I think it's comparing pointers or something like that.

Share this post


Link to post
Share on other sites

The problem is not with the portion of code you have posted. The problem lies elsewhere e.g. in the getRank method or somewhere else.

Post more code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement