首页 > 解决方案 > 使用泛型方法时“没有匹配的函数调用”

问题描述

我正在尝试使用使用 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”没有匹配的函数

标签: c++

解决方案


正如评论所暗示的,你有两个问题。一是缺少括号。你需要这个:

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*并进行强制转换。


推荐阅读