Jump to content
  • Advertisement
Sign in to follow this  
CastorX

Function pointer

This topic is 4551 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

Hy! I'm trying to sort an array with stdlib's qsort(...) function. So,I need a function pointer and calling: we are int the CParticleSystem's (class) SortParticles function! int (*PT2CMP)(const void *,const void *) = &CParticleSystem::compfunc; qsort(DrawIndices, this->Data.Size, sizeof(unsigned short),PT2CMP); ERROR: error C2440: 'initializing' : cannot convert from 'int (__thiscall CParticleSystem::* )(const void *,const void *)' to 'int (__cdecl *)(const void *,const void *)' What can I do now? Why isn't it OK? I visited the function-pointer.org, but... Bye & Thanks!

Share this post


Link to post
Share on other sites
Advertisement
qsort() accepts a non-member function, You tried passing it a member function. This doesn't work. Since you'll need to rewrite your comparison function anyways, consider using std::sort().

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
qsort() accepts a non-member function, You tried passing it a member function. This doesn't work. Since you'll need to rewrite your comparison function anyways, consider using std::sort().


Could you not do this with the use of a Functor?

Share this post


Link to post
Share on other sites
Quote:
Original post by Richy2k
Quote:
Original post by SiCrane
qsort() accepts a non-member function, You tried passing it a member function. This doesn't work. Since you'll need to rewrite your comparison function anyways, consider using std::sort().


Could you not do this with the use of a Functor?


You could. But it's as easy to use a function, but less readable with a functor( IMO ).

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
qsort() accepts a non-member function, You tried passing it a member function. This doesn't work. Since you'll need to rewrite your comparison function anyways, consider using std::sort().


What if he made it static? Could it work then?

Share this post


Link to post
Share on other sites
static functions are not member functions and work just like any other non-member function. You can pass those as function pointers so that should work properly.

Share this post


Link to post
Share on other sites
You are using C++ so why are you using the C standard libary function qsort instead of the C++ standard library function std::sort?

Share this post


Link to post
Share on other sites
Quote:
Original post by Richy2k
Quote:
Original post by SiCrane
qsort() accepts a non-member function, You tried passing it a member function. This doesn't work. Since you'll need to rewrite your comparison function anyways, consider using std::sort().


Could you not do this with the use of a Functor?


If he could create a functor, he'd be using C++, and if he were using C++, he should be using std::sort instead of qsort anyway. (std::sort can be applied to arrays just fine: pointers are iterators, so to sort the array "int foo[SIZE]" you can do "std::sort(foo, foo + SIZE)".)

Edit: Oh, and dalleboy is correct; there is evidence of C++ usage here - specifically the reference to "__thiscall" in the error message.

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!