Function pointer
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!
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().
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?
Quote:Original post by Richy2kQuote: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 ).
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?
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.
You are using C++ so why are you using the C standard libary function qsort instead of the C++ standard library function std::sort?
Quote:Original post by Richy2kQuote: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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement