Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
5 replies to this topic

#1 NewVoxel   Members   -  Reputation: 320

Like
0Likes
Like

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


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12920

Like
1Likes
Like

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.

#3 Pink Horror   Members   -  Reputation: 1144

Like
0Likes
Like

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.



#4 Endurion   Crossbones+   -  Reputation: 3462

Like
0Likes
Like

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>

#5 iMalc   Crossbones+   -  Reputation: 2299

Like
0Likes
Like

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

#6 Angus Hollands   Members   -  Reputation: 713

Like
0Likes
Like

Posted 21 April 2014 - 06:43 PM

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






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.



PARTNERS