Reputation: 5086
Here's a template I made. I believe it is self-explanatory
template <class N, class I>
std::list<N*> selectiveList(I element, std::list<N*> & container, I (N::*f)() const) {
typename std::list<N*>::iterator it;
std::list<N*> selectiveListing;
for (it = container.begin(); it != container.end(); ++it)
if ((*it)->*f == element)
selectiveListing.push_back(*it);
if (selectiveListing.size() == 0)
throw NoItemsFound<I>(element);
return selectiveListing;
}
I call it from this function:
void AirportManager::searchAirplanesTypes() {
clrscr();
std::stringstream prompt;
prompt << "Escolha atributo a pesquisar: \n" <<
"1) Tipo\n" <<
"2) Descricao\n" << "3) Categoria\n";
int choice = getNumberInput(prompt.str(), 1, 3);
switch (choice) {
case 1: {
std::string searchElement1 = getStringInput("Tipo: ");
pageNav(selectiveList(searchElement1, airport.airplanesTypes,
&AirplaneType::getType), "", true, true);
break;
}
case 2: {
std::string searchElement2 = getStringInput("Descricao: ");
pageNav(selectiveList(searchElement2, airport.airplanesTypes,
&AirplaneType::getDescription), "", true, true);
break;
}
case 3: {
//Category is an enumerated data type
Category searchElement3 = getCategoryInput("Categoria: ");
pageNav(selectiveList(searchElement3, airport.airplanesTypes,
&AirplaneType::getCategory), "", true, true);
break;
}
}
}
I really can't see anything wrong. Yet when I compile it, here's what happens:
In file included from ..\src\AirportManager.cpp:14:0:
..\src\/headers/Template.h: In function 'std::list<N*> selectiveList(I, std::list<N*>&, I (N::*)()const) [with N = AirplaneType, I = std::basic_string<char>]':
..\src\AirportManager.cpp:1259:34: instantiated from here
..\src\/headers/Template.h:340:3: error: invalid use of non-static member function
..\src\/headers/Template.h: In function 'std::list<N*> selectiveList(I, std::list<N*>&, I (N::*)()const) [with N = AirplaneType, I = Category]':
..\src\AirportManager.cpp:1265:31: instantiated from here
..\src\/headers/Template.h:340:3: error: invalid use of non-static member function
These are a bit random. I can't see how statics come into play here. All the getters referenced are simple const returns.
I'd be happy to get some input.
Upvotes: 0
Views: 404
Reputation: 58677
The canonical way to copy elements is to use a negated std::remove_copy_if()
or write a copy_if()
algorithm. IMHO copy_if()
is the clearer alternative, and eventhough it is not a standard function it is very easy to write properly:
template<class In, class Out, class Pred>
Out std::copy_if(In first, In last, Out res, Pred p)
{
while( first != last ) {
if( p(*first) ) *res++ = *first;
++first;
}
return res;
}
You can then use it like so:
if( choice == 1 ) {
copy_if(airport.airplanesTypes.begin(), airport.airplanesTypes.end(),
std::back_inserter(result),
boost::bind(&AirplaneType::getType, _1, searchElement1));
pageNav(result, "", true, true);
} else if( choice == 2 ) { // ...
The nice thing here is that you can use copy_if()
for plenty of other things. It works with any container, not just std::list<N*>
and it works with any unary predicate, whether a primitive function or a function object.
Upvotes: 1
Reputation: 70523
It has been a while, but are you missing a new?
throw new NoItemsFound<I>(element);
Upvotes: 0