0

I have the following code:

int partition(void* arr, int start, int end, bool(*cmp_f)(void*, void*),
              void(*swap_f)(void*, void*)) {
//    Point *pivot = &pts[end];
    int partition_index = start;

    for (int i = start; i < end; i++) {
        if (cmp_f(&arr[i], &arr[end])) {// <---------- Here
            swap_f(&arr[i], &arr[partition_index]);// <---------- Here
            partition_index++;
        }
    }
    swap_f(&arr[end], &arr[partition_index]);// <---------- Here
    return partition_index;
}
//------------------------------------------------------------------------------
void quick_sort(Point* pts, int start, int end, bool(*cmp_f)(void*, void*),
                void(*swap_f)(void*, void*)) {
    if (start < end) {//As long start is less than end we should arrange
        int pivot = partition(pts, start, end, cmp_f, swap_f);

        quick_sort(pts, start, pivot - 1, cmp_f, swap_f);
        quick_sort(pts, pivot + 1, end, cmp_f, swap_f);
    }
}
//------------------------------------------------------------------------------

and I get the following error:

error: ‘void*’ is not a pointer-to-object type

by looking I have found the following answer:

As the compiler message says, void* is not a pointer to object type. What this means is that you cannot do anything with void*, besides explicitly converting it back to another pointer type. A void* represents an address, but it doesn’t specify the type of things it points to, and at a consequence you cannot operate on it.

source:In C++, I'm getting a message "error: 'void*' is not a pointer-to-object type"

The error is caused by the following lines:

cmp_f(&arr[i], &arr[end]);
swap_f(&arr[i], &arr[partition_index]);
swap_f(&arr[end], &arr[partition_index]);

Manual casting wouldn't be helpful for my code Now my question is how i can pass arr[index] to cmp_f or swap_f without manual casting ?

Bahaa Zahika
  • 117
  • 2
  • 11
  • 3
    This is basically C. If you are using C++, embrace C++ and use templates; this will fix the issue – Justin Jun 20 '17 at 05:28
  • Your function is missing information about the size of the objects in the buffer. You can't dereference a void pointer (with `arr[i]`) for instance. The problem in your code is too fundamental and will require a re-write. So I'm voting to close as too broad. – StoryTeller - Unslander Monica Jun 20 '17 at 05:38
  • Take a read of a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and you will see how to do this with templates – Passer By Jun 20 '17 at 05:39
  • thanks, I am still newbie in C++, ill take a look at templates. – Bahaa Zahika Jun 20 '17 at 05:50

1 Answers1

0

You can't pass the array as void* because the element size is not known in this case and thus you're not able to access your elements. You have to change the signature of the partition function as following:

int partition(Point* arr, int start, int end, bool(*cmp_f)(Point*, Point*),
  void(*swap_f)(Point*, Point*));

If partition() must support multiple types use templates as suggested in the comments:

template<typename T> int partition(T* arr, int start, int end, bool(*cmp_f)(T*, T*),
  void(*swap_f)(T*, T*));

template<typename T> void quick_sort(T* pts, int start, int end, bool(*cmp_f)(T*, T*),
  void(*swap_f)(T*, T*));
candidus
  • 119
  • 6