error: ‘void*’ is not a pointer-to-object type what is the right solution?
error: ‘void*’ is not a pointer-to-object type what is the right solution?
我有以下代码:
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);
}
}
//------------------------------------------------------------------------------
我收到以下错误:
错误:“void*”不是指向对象类型的指针
通过查找我找到了以下答案:
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.
来源:
错误是由以下几行引起的:
cmp_f(&arr[i], &arr[end]);
swap_f(&arr[i], &arr[partition_index]);
swap_f(&arr[end], &arr[partition_index]);
手动转换对我的代码没有帮助现在我的问题是如何在没有手动转换的情况下将 arr[index] 传递给 cmp_f 或 swap_f?
您不能将数组作为 void* 传递,因为在这种情况下元素大小未知,因此您无法访问您的元素。您必须如下更改分区函数的签名:
int partition(Point* arr, int start, int end, bool(*cmp_f)(Point*, Point*),
void(*swap_f)(Point*, Point*));
如果 partition() 必须支持多种类型,请按照评论中的建议使用模板:
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*));
我有以下代码:
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);
}
}
//------------------------------------------------------------------------------
我收到以下错误:
错误:“void*”不是指向对象类型的指针
通过查找我找到了以下答案:
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.
来源:
错误是由以下几行引起的:
cmp_f(&arr[i], &arr[end]);
swap_f(&arr[i], &arr[partition_index]);
swap_f(&arr[end], &arr[partition_index]);
手动转换对我的代码没有帮助现在我的问题是如何在没有手动转换的情况下将 arr[index] 传递给 cmp_f 或 swap_f?
您不能将数组作为 void* 传递,因为在这种情况下元素大小未知,因此您无法访问您的元素。您必须如下更改分区函数的签名:
int partition(Point* arr, int start, int end, bool(*cmp_f)(Point*, Point*),
void(*swap_f)(Point*, Point*));
如果 partition() 必须支持多种类型,请按照评论中的建议使用模板:
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*));