我有一个非常基本的问题,试图帮助一个朋友,但是我现在找不到解决方案,所以也许有人在这里可以提供帮助,谢谢!
问题是:我们试图使用带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”
答案 0 :(得分:1)
正如评论所建议的,您有两个问题。一种是缺少括号。您需要这个:
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
需要一个返回void*
的函数,则必须在某个地方 一个返回void*
的函数。您可以编写包装函数,将返回的struct Circle*
接受并转换。