在另一个 class 的函数中使用一个 class 的函数指针
Using a function pointer from one class in a function of another class
我查看了以下主题,但它们似乎对解决问题没有帮助:
Function pointer to member function - 这个线程没有帮助,因为访问是在 main 而不是另一个 class
one class should invoke method of another class using a function pointer - 我无法遵循已接受的答案,因为我不清楚如何使用 <functional>
header.
我有一个 Useful Function class, UF
,我在其中声明和定义我的程序的所有常用实用程序功能。这些功能之一是
int UF::compar_dbl_asc(const void *a, const void *b)//comparator for sorting
{ //array of double in ascending order
int aa = *((int *)a), bb = *((int *)b);
if (base_arr_dbl[aa] < base_arr_dbl[bb])
return -1;
if (base_arr_dbl[aa] == base_arr_dbl[bb])
return 0;
if (base_arr_dbl[aa] > base_arr_dbl[bb])
return 1;
}
这是我打算在 qsort()
中使用的比较器函数,用于按升序对双打进行排序。
在 UF
class 定义中,我也有 double* base_arr_dbl;
作为声明。这是 qsort 将使用的值(双精度)数组。
现在,在另一个名为 SEP
的 class 中,我有一个函数 fn1
,其中我想让 base_arr_dbl
指向本地 (到 fn1
) 双数组,我想使用 UF
中的比较器函数调用 qsort
。非常具体,虽然这可能不需要,但我不对实际值的数组进行排序,而是对索引数组 sortedindices[]
进行排序,这样 sortedindices[0]
将保存中最小条目的索引base_arr_dbl[]
数组。即排序后的顺序为base_arr_dbl[sortedindices[0]], base_arr_dbl[sortedindices[1]]
,以此类推
所以,我这样做:
void SEP::fn1(UF *uf) {
double ratio[100];
//populate ratio's entries
//Now, sort ratio's entries.
uf->set_base_arr_dbl(ratio); //This function is defined as
//void UF::set_base_arr_dbl(double *ptr) { base_arr_dbl = ptr; }
qsort(sortedindices, 100, sizeof(double), uf->compar_dbl_asc);
}
但是,行 qsort(sortedindices, 100, sizeof(double), uf->compar_dbl_asc);
抛出以下编译时错误:
error C3867: 'USEFUL_FUNCTIONS::compar_dbl_asc': non-standard syntax; use '&' to create a pointer to member
我试过 &uf->compar_dbl_asc
但出现错误:
error C2276: '&': illegal operation on bound member function expression
任何帮助解决此问题的方法都将受到赞赏。
正如编译器在错误消息中明确指出的那样,uf->compar_dbl_asc
和 &(uf->compar_dbl_asc)
都不适合用作 qsort
.
的参数
您可以使用以下方法之一将 compar_dbl_asc
作为 qsort
的参数。
- 使
compar_dbl_asc
成为class的static
成员函数。
- 更好的是,为您的应用程序创建一个
namespace
并在 namespace
中定义 compar_dbl_asc
。 UF
可能是 namespace
除非出于某些其他原因它必须是 class。
另一种选择是放弃使用 qsort
,转而使用 std::sort
。后者为您提供了更多选择。您可以在使用 std::sort
时使用仿函数或 lambda 函数,这将允许您使用 UF::compar_dbl_asc
.
std::sort(sortedindices, sortedindices+100,
[=uf](int a, int b) { return uf->compar_dbl_asc(a, b); });
需要注意的一点是,如果您选择最后一种方法,UF::compar_dbl_asc
的签名可以更改为更加用户友好的变体。
bool UF::compar_dbl_asc(int a, int b)
return (base_arr_dbl[a] < base_arr_dbl[b]);
}
我查看了以下主题,但它们似乎对解决问题没有帮助:
Function pointer to member function - 这个线程没有帮助,因为访问是在 main 而不是另一个 class
one class should invoke method of another class using a function pointer - 我无法遵循已接受的答案,因为我不清楚如何使用 <functional>
header.
我有一个 Useful Function class, UF
,我在其中声明和定义我的程序的所有常用实用程序功能。这些功能之一是
int UF::compar_dbl_asc(const void *a, const void *b)//comparator for sorting
{ //array of double in ascending order
int aa = *((int *)a), bb = *((int *)b);
if (base_arr_dbl[aa] < base_arr_dbl[bb])
return -1;
if (base_arr_dbl[aa] == base_arr_dbl[bb])
return 0;
if (base_arr_dbl[aa] > base_arr_dbl[bb])
return 1;
}
这是我打算在 qsort()
中使用的比较器函数,用于按升序对双打进行排序。
在 UF
class 定义中,我也有 double* base_arr_dbl;
作为声明。这是 qsort 将使用的值(双精度)数组。
现在,在另一个名为 SEP
的 class 中,我有一个函数 fn1
,其中我想让 base_arr_dbl
指向本地 (到 fn1
) 双数组,我想使用 UF
中的比较器函数调用 qsort
。非常具体,虽然这可能不需要,但我不对实际值的数组进行排序,而是对索引数组 sortedindices[]
进行排序,这样 sortedindices[0]
将保存中最小条目的索引base_arr_dbl[]
数组。即排序后的顺序为base_arr_dbl[sortedindices[0]], base_arr_dbl[sortedindices[1]]
,以此类推
所以,我这样做:
void SEP::fn1(UF *uf) {
double ratio[100];
//populate ratio's entries
//Now, sort ratio's entries.
uf->set_base_arr_dbl(ratio); //This function is defined as
//void UF::set_base_arr_dbl(double *ptr) { base_arr_dbl = ptr; }
qsort(sortedindices, 100, sizeof(double), uf->compar_dbl_asc);
}
但是,行 qsort(sortedindices, 100, sizeof(double), uf->compar_dbl_asc);
抛出以下编译时错误:
error C3867: 'USEFUL_FUNCTIONS::compar_dbl_asc': non-standard syntax; use '&' to create a pointer to member
我试过 &uf->compar_dbl_asc
但出现错误:
error C2276: '&': illegal operation on bound member function expression
任何帮助解决此问题的方法都将受到赞赏。
正如编译器在错误消息中明确指出的那样,uf->compar_dbl_asc
和 &(uf->compar_dbl_asc)
都不适合用作 qsort
.
您可以使用以下方法之一将 compar_dbl_asc
作为 qsort
的参数。
- 使
compar_dbl_asc
成为class的static
成员函数。 - 更好的是,为您的应用程序创建一个
namespace
并在namespace
中定义compar_dbl_asc
。UF
可能是namespace
除非出于某些其他原因它必须是 class。
另一种选择是放弃使用 qsort
,转而使用 std::sort
。后者为您提供了更多选择。您可以在使用 std::sort
时使用仿函数或 lambda 函数,这将允许您使用 UF::compar_dbl_asc
.
std::sort(sortedindices, sortedindices+100,
[=uf](int a, int b) { return uf->compar_dbl_asc(a, b); });
需要注意的一点是,如果您选择最后一种方法,UF::compar_dbl_asc
的签名可以更改为更加用户友好的变体。
bool UF::compar_dbl_asc(int a, int b)
return (base_arr_dbl[a] < base_arr_dbl[b]);
}