qsort in class

Started by
7 comments, last by izhbq412 18 years, 3 months ago
I have a class type "player_group" with a member "group". One of the variables of "player_group" is a struct "unit" and the variable is "units":

class player_group{
public:
 ...
 unit units[10];
 ...
}group;

Now, inside one of the functions of the "player_group" I want to sort the units and I do:

qsort((void *) &unit, number_of_units, sizeof(struct unit), (compfn)CompareDis );

And I get the error: parse error before ',' token (DevC++) Does anyone know what the problem is cause it's driving me crazy!! Thanks a lot!!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
Wait, I think I foud the problem , for the first term should I have written
&units ?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
#include <algorithm> // sort....bool CompareDis(const unit& lhs, const unit& rhs) { ... }....std::sort(units, units + 10, CompareDis);....

Don't use qsort in C++. It's ancient and not typesafe, and also hasn't likely been worked on in ages (i.e. the smart people at compiler writing places that are interested in things like optimizing the sorting algorithm will be working on std::sort rather than qsort). And it was only ever available for backwards compatibility anyway - it comes from the C standard library.

Also, what's the '10' doing there in your declaration? It should be number_of_units, surely? Unless perhaps you're doing something silly like not necessarily using all the elements? You might want to use std::vector and save headaches rather than prematurely optimizing; arrays are evil.

And how is it that your class is named "player_group" and the instance (not 'member'; 'member' is what "units" is) is named "group"? that seems backwards to me.
Wow, thanks a lot Zahlman. I'm, as in the article a pretty experianced C programmer and a while ago I went to C++. I didn't realize that there were so many new things I could use. Could you please give me more or a link with more? Thanks!!!!

(Could you or someone else explain how to use vectors instead of arrays?)

[Edited by - daniel_i_l on January 1, 2006 3:50:28 PM]
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Get the book "The C++ Standard Library : A Tutorial and Reference"

And std::sort is much faster because it's a template and thus your compiler is able to optimize it for your types (qsort is always working with void*)
I think that in later projects I'll switch to the STL but for now I'm more comfortable with qsort() - Thanks for your patiance.
I'm sorry to keep bugging you with this qsort but I got one more problem:
I have a class:
class player_group{public: player_group(); unit units[10]; unit mid_pos; grid main_path[500]; ... int CompareDis(struct unit *elem1, struct unit *elem2); int CompareDisMid(struct unit *elem1, struct unit *elem2); void SortUnits(int method); ...};


and the funcs:
int player_group :: CompareDis(struct unit *elem1, struct unit *elem2)		   {   if(abs(elem1->next_step.x-main_path[place_on_path+step].x)+      abs(elem1->next_step.y-main_path[place_on_path+step].y)     <abs(elem2->next_step.x-main_path[place_on_path+step].x)+      abs(elem2->next_step.y-main_path[place_on_path+step].y))			      return -1;											   else if (abs(elem1->next_step.x-main_path[place_on_path+step].x)+            abs(elem1->next_step.y-main_path[place_on_path+step].y)           >abs(elem2->next_step.x-main_path[place_on_path+step].x)+            abs(elem2->next_step.y-main_path[place_on_path+step].y))						      return 1;					  				      else        return 0;	    										   }int player_group :: CompareDisMid(struct unit *elem1, struct unit *elem2)	  {   if(abs(elem1->current_step.x-mid_pos.current_step.x)+      abs(elem1->current_step.y-mid_pos.current_step.y)     <abs(elem2->current_step.x-mid_pos.current_step.x)+      abs(elem2->current_step.y-mid_pos.current_step.y))			      return -1;												   else if(abs(elem1->current_step.x-mid_pos.current_step.x)+           abs(elem1->current_step.y-mid_pos.current_step.y)        >  abs(elem2->current_step.x-mid_pos.current_step.x)+           abs(elem2->current_step.y-mid_pos.current_step.y))			      return 1;													   else																       return 0;	    												}void player_group :: SortUnits(int method){  if(method==0)  {   qsort((void *) &units, number_of_units, sizeof(struct unit), (compfn)CompareDis );  }  if(method==1)  {  qsort((void *) &units, number_of_units, sizeof(struct unit), (compfn)CompareDisMid );  }}


This is supposed to sort the units in a group by one of the methods. When I try to compile I get the error: "no matches converting CompareDis to type int"
same for the CompareDisMid. What is the problem?
Thanks a lot to everyone!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
The problem is, you're using a C compatibility feature in a (semi-)modern programming language. qsort was not written to work properly with member functions. If you want to use qsort, you'll need to use a global comparison function or a static member function.
You can't use non-static member functions because they have a hidden argument - a pointer to the instance of the class (the this pointer). Every operation over the non-static class data goes through the this pointer, it's just that you don't see it :)
_______________The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper

This topic is closed to new replies.

Advertisement