3

伙计们!我正在尝试这样做:

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

但我收到一个错误:“filterFunc”不是一种类型

我正在尝试这样做,因为我想要一个通用过滤器,所以我可以这样做:

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;
}

我怎么解决这个问题?

4

1 回答 1

9

您忘记了typedef, 来声明类型。否则,这个声明只会创建一个类型为 的变量int(*)(Medicine*,Medicine*)

  typedef int (*filterFunc)(Medicine* criteria, Medicine*);
//^^^^^^^
于 2013-04-06T10:27:46.737 回答