Function pointer

Started by
7 comments, last by Zahlman 18 years, 2 months ago
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!
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().
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?
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
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 ).
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?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
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?
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
I will try the std::sort function. THX
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.

This topic is closed to new replies.

Advertisement