user1849859
user1849859

Reputation: 319

pointer to function 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?

Upvotes: 3

Views: 63

Answers (1)

kennytm
kennytm

Reputation: 523694

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*);
//^^^^^^^

Upvotes: 9

Related Questions