I am trying to make a more general filter so I defined with typedef a pointer to function. But I receive an error in the DynamicVector* filterByQuantity(int quantity) function. This is the whole code for what I am trying to do:
controller.h
typedef int (*filterFunc)(Medicine* criteria, Medicine*);
DynamicVector<Medicine>* filter2(Medicine* criteria, filterFunc f);
controller.cpp
#include "controller.h"
int filterByQuantityLess(Medicine* criteria, Medicine* p) {
if (criteria->getQuantity() > p->getQuantity()) {
return 0;
}
return 1;
}
DynamicVector<Medicine>* Controller::filter2(Medicine* criteria, filterFunc f){
DynamicVector<Medicine>* medList = repo->getAll();
DynamicVector<Medicine>* rez = new DynamicVector<Medicine>();
for(int i=0; i < medList->getLen(); i++){
Medicine* m = new Medicine(medList->getElementAtPosition(i));
if(!f(criteria, m)){
rez->addElement(*m);
}
}
return rez;
}
DynamicVector<Medicine>* filterByQuantity(int quantity){
Medicine* criteria = new Medicine(1,"",1,quantity);
DynamicVector<Medicine>* rez = filter2(criteria, filterByQuantityLess); //error
return rez;
}
the error is: Multiple markers at this line - 'filter2' was not declared in this scope - Function 'filter2' could not be resolved
Where am I doing wrong? The filter2 function returns a DynamicVector so I don't understand why it doesn't work.
Because filter2´s declaration and its implementation differs.
Look at how you're prototyping it on line 2 and how you implements it. Filter2
should either be declared as part of the Controller
scope or as part of the global scope, not both.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.