c++ - 使用泛型方法时“没有匹配的函数调用”
问题描述
我正在尝试使用使用 void 的原始泛型方法,问题是,泛型函数获取指向函数的指针并返回 void*(我将立即显示代码)
我试图改变语法几次。我认为堆栈或堆有问题,我不确定。
void *get_structs_of_area(void *arr,
double area,
void*(*Allocation)(int size),
void*(*Get(void*, int)),
void(*Add(void*, void*)),
void(*Nullized(void*)),
double (* get_area)(void *))
{
void* newArray = Allocation(N);
Nullized(newArray);
int cellUsed = 0;
for(int i = 0; i<N; i++)
if(get_area(Get(arr, i)) <=area)
{
Add(Get(arr, i), Get(newArray, cellUsed));
cellUsed++;
}
return newArray;
}
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
struct Circle* CircleAllocation(int size)
{
struct Circle* tmp = new(nothrow)struct Circle[size];
if(tmp == NULL)
{
std::cerr << "Allocation Failed!" << std::endl;
exit(EXIT_FAILURE);
}
return tmp;
}
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
void AddCircle(void* arr, void*newArr)
{
struct Circle* x = (struct Circle*) arr;
struct Circle* y = (struct Circle*) newArr;
y = x;
}
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
struct Circle* GetCircle(void* arr, int index)
{
return (struct Circle*)arr+index;
}
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
double getCirArea(void* cir)
{
struct Circle* tmp = (struct Circle*)cir;
return (tmp->_r * tmp->_r * 3.141);
}
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
void NullCircle(void*arr)
{
for(int i = 0; i<N; i++)
{
struct Circle* x = (struct Circle*)GetCircle(arr, i);
x = NULL;
}
}
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
void FreeCircle(void* arr)
{
for(int index = 0; index < N; index++)
{
struct Circle* x = (struct Circle*)GetCircle(arr, index);
delete[] x;
}
}
/*-----------------------------------------------------------------------------------*/
int main() {
//example for arr of circle //
struct Circle cirArr[N];
cirArr[0]._center._x = 3;
cirArr[0]._center._y = 3;
cirArr[0]._r = 13;
cirArr[1]._center._x = 17;
cirArr[1]._center._y = 12;
cirArr[1]._r = 5;
//example for calling the function//
struct Circle* newRec = (struct Circle*)get_structs_of_area(cirArr,
3.3,
CircleAllocation,
GetCircle,
AddCircle,
NullCircle,
getCirArea);
return EXIT_SUCCESS;
}
消息错误:
调用“get_structs_of_area”没有匹配的函数
解决方案
正如评论所暗示的,你有两个问题。一是缺少括号。你需要这个:
void *get_structs_of_area(void *arr,
double area,
void*(*Allocation)(int size),
void*(*Get)(void*, int),
void((*Add)(void*, void*)),
void((*Nullized)(void*)),
double (* get_area)(void *))
另一个是类型不匹配。如果get_structs_of_area
需要一个返回 a 的函数,那么void*
某处必须有一个返回 a 的函数void*
。您可以编写包装函数来获取返回值struct Circle*
并进行强制转换。
推荐阅读
- javascript - 将 nuxt pwa 功能与 fcm 一起使用的最佳解决方案是什么?
- python - 如何过滤 Dataframe 列中的时间以输出行并计算另一行的不同值?
- powershell - 为什么 select-object -first 1 不返回数组以保持一致性?
- python - 如何修复:对views.py函数进行单元测试时出现“TypeError:预期的字符串或类似字节的对象”
- microsoft-translator - 自定义翻译器 - 上传后文件不可用
- javascript - InDesign 脚本搜索单词并打印页码
- python - 如何从具有不同大小的另一个数据框将列添加到数据框
- c# - 如何摆脱winform的插件安全性
- php - 在 Symfony 中创建新的文件夹结构
- android - 将arraylist保存到sharedpreferences kotlin的方法