Sign in to follow this  

Function pointer

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

This topic is 4305 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this