Debug assertion in C++ on code ported from VC6 to Visual Studio 2005 ?

_Scotch_ used Ask the Experts™
Greetings Experts.  I have lots of sort compare code that looks like this:

Everything was great ten years ago or whenever this was written and today it barely builds and runs.  Right now I'm working on debug assertions (invalid operator <).   The problem seems to be hinted at by MSFT here:

but I can't see that I'm doing anything different?
stable_sort(vCollars.begin(), vCollars.end(), Collar_Compare_Less);

And a predicate routine:

bool CMainFrame::Collar_Compare_Less(Collar* a, Collar* b)  {

	TRACE("Option a: %x, b: %x --- %d\r\n", a, b, ++bFoo);

	if (strcmp(a->cc->callSold->stock, b->cc->callSold->stock) == 1)
		return true;

	if (a->cc->callSold->strike > b->cc->callSold->strike)
		return true;

	if (a->cc->callSold->dtg > b->cc->callSold->dtg)
		return true;

	if (a->premium_percent > b->premium_percent)
		return true;

	return false;

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think the problem is that you need equivalent < statements for each of those > statements you have.  For example,

if (a->premium_percent > b->premium_percent)
            return true;

You need to also have:
if (a->premium_percent < b->premium_percent)
            return false;
My blind guess... Is the CMainFrame::Collar_Compare_Less() a static member of the CMainFrame class?  Or even the normal method of the class?  Try to make it a plain function.
>>if (strcmp(a->cc->callSold->stock, b->cc->callSold->stock) == 1)
strcmp() returns 0 if two strings are equal. Else non zero.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Not exactly. The strcmp() indicates also what of the strings is greater/smaller then the other.  The truth is that it says nothing about 1:

A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

So that you may wanted to write > 0 instead of == 1

IMO Kelaros is right - just  a bit more more detailed explanation: the problem is that with the original code it's possible to generate cases where for two elements 'a' and 'b' the compare function returns true for both 'a > b' and 'b > a', thus it's impossible for 'stable_sort' to find a real unambiguous sort order.

For example if you have to elements in you container which have data like this:
a->cc->callSold->strike = 10;
b->cc->callSold->strike = 20;
a->cc->callSold->dtg = 20;
b->cc->callSold->dtg = 10;

Then both calls 'Collar_Compare_Less( &a, &b )' and 'Collar_Compare_Less( &b, &a )' return true (as long as the string compare doesn't return earlier - here I agree with the last two comments.

If you add the complementary comparison as suggested by Kelaros this cannot happen anymore sonce only if for the first 'if' the elements are treated as equal further comparisons are done ...

Hope that helps,



sorry for the delay

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial