Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

qsort in class

This topic is 4609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!!

Share this post


Link to post
Share on other sites
Advertisement

#include <algorithm> // sort
....
bool CompareDis(const unit& lhs, const unit& rhs) { ... }
....
std::sort(units, units + 10, CompareDis);
....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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*)

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!