View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Over ridden comparison operator returning false comparisons

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1NewVoxel  Members

Posted 18 April 2014 - 08:25 AM

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.

Your authority is not recognized in Fort Kick-ass http://www.newvoxel.com

### #2Álvaro  Members

Posted 18 April 2014 - 08:31 AM

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.

### #3Pink Horror  Members

Posted 18 April 2014 - 10:05 AM

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.

### #4Endurion  Members

Posted 18 April 2014 - 10:18 AM

Also, since your enum values have quite common names, double check that there's no #define interfering.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

### #5iMalc  Members

Posted 18 April 2014 - 07:45 PM

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.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

### #6Angus Hollands  Members

Posted 21 April 2014 - 06:43 PM