0

I am using a pointer to a template member function as follows in my "Array" class :

//Sorts elements according to the value of sortFn (ascending)
template<typename T>
template<typename S>
void Array<T>::sort(int (S::*sortFn)())
{
    quickSort(0, size - 1, &S::sortFn);
}

//Sorts elements according to the value of sortFn (ascending) using quicksort.
template<typename T>
template<typename S>
void Array<T>::quickSort(int start, int pivot, int (S::*sortFn)())
{
    if (start >= pivot)
        return;
    int current = start;
    int wall = start;
    while (current != pivot)
        if ((arr[current]->*sortFn)() < (arr[pivot]->*sortFn)())
        {
            current++;
        }
        else
        {
            swap(arr[current], arr[wall]);
            current++;
            wall++;
        }
    swap(arr[wall], arr[pivot]);
    quickSort(start, wall - 1, &S::sortFn);
    quickSort(wall + 1, pivot, &S::sortFn);

}

So this is just a sort function based on quicksort, that takes an extra function pointer parameter and sorts the array according to the value of this function (called by each element).

I have been using similar implementations throughout the program and they work fine, however, when I try calling this sort function (enemiesList is an object of class Array , getFD is a member function of class Enemy that returns int) :

enemiesList.sort(&Enemy::getFD);

I get these errors :

'sortFn' is not a member of 'Enemy'

Array::quickSort: function does not take 3 arguments

erros

At this line of the sort function :

quickSort(0, size - 1, &S::sortFn);

Please note that there are overloaded versions of sort & quicksort that are implemented exactly the same but without using the function pointer parameter (using < for comparison). Any help??


Edit: I managed to fix the error by replacing the quickSort function call inside sort function to :

quickSort(0, size - 1, sortFn);

Also the 2 recursive call lines inside quickSort:

quickSort(start, wall - 1, sortFn);
quickSort(wall + 1, pivot, sortFn);

I kinda understood that the compiler couldn't tell that sortFn passed to "sort" as an argument wasn't the same as sortFn passed to "quickSort" inside of it. I still don't really understand why this happens and HOW EXACTLY SHOULD I USE templates, function pointers, and classes together ? Can someone please explain to me the right way to do things and how ?

Thanks in advance.

5
  • I want to understand why when I pass the getFD function I have to use a "&" like this : &Enemy::getFD However when I pass sortFn inside sort I don't use it, like this: quickSort(.. , .. , sortFn)
    – Yosry
    Apr 28, 2018 at 3:56
  • sortFn is a wrong name for a getter (used for comparison).
    – Jarod42
    Apr 28, 2018 at 4:13
  • @Jarod42 sortFn is not a getter, it's just the argument name
    – Yosry
    Apr 28, 2018 at 9:02
  • I mean that sortFn doesn't sort (as quicksort/std::sort do), it is not a comparer function/functor (as std::less/std::greater are) but a projection used by comparison (as getAge/computeScore).
    – Jarod42
    Apr 30, 2018 at 7:53
  • Alright got it, thanks !
    – Yosry
    Apr 30, 2018 at 10:53

1 Answer 1

1
quickSort(0, size - 1, &S::sortFn);

should simply be:

quickSort(0, size - 1, sortFn);

Same in recursive call:

quickSort(start, wall - 1, sortFn);
quickSort(wall + 1, pivot, sortFn);

sortFn is your variable, &S::sortFn is (after template substitution) &Enemy::sortFn, a pointer on (non existent) member sortFn.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.