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 ?