Over ridden comparison operator returning false comparisons

Started by
4 comments, last by Angus Hollands 9 years, 12 months ago

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

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.


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.

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>

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

Are your cards inheriting? If so, is your getRank method virtual?

This topic is closed to new replies.

Advertisement