Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer to function c++

Tags:

c++

guys! I am trying to do this:

int (*filterFunc)(Medicine* criteria, Medicine*);
DynamicVector<Medicine>* filter2(Medicine* criteria, filterFunc f); //error

but I get an error: 'filterFunc' is not a type

I am trying to do this because I want a general filter so then I can do this:

int filterPrice(Pet* pet) {
    if (pet->price > 10 && pet->price < 100) {
        return 0;
    }
    return 1;
}

VectorDinamic* filter2(Pet* criteria, filterFunc f) {
    VectorDinamic* v = getAll(ctr->repo);
    VectorDinamic* rez = creazaVectorDinamic();
    int nrElems = getNrElemente(v);
    int i;
    for (i = 0; i < nrElems; i++) {
        Pet* pet = get(v, i);
        if (!f(criteria, pet)) {
            add(rez, copyPet(pet));
        }
    }
    return rez;
}

VectorDinamic* filterByPrice(float price) {
    Pet* criteria = createPet(1, "", "", price);
    VectorDinamic* rez = filter2(ctr, criteria, filterByPriceGr);
    destroyPet(criteria);
    return rez;
}

How can I solve this problem?

like image 558
user1849859 Avatar asked Sep 01 '26 08:09

user1849859


1 Answers

You forgot a typedef, to declare the type. Otherwise this declaration just creates a variable of type int(*)(Medicine*,Medicine*).

  typedef int (*filterFunc)(Medicine* criteria, Medicine*);
//^^^^^^^
like image 145
kennytm Avatar answered Sep 03 '26 23:09

kennytm