3

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?

1 Answer 1

9

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*);
//^^^^^^^
Sign up to request clarification or add additional context in comments.

1 Comment

If you remember that any declaration can be turned into a type using typedef, it's much easier to read and write: typedef int filter_function(medicine*, medicine*) which you can then use as filter_function* f, which also doesn't blur the fact that it's a pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.