这是我制作的模板。我认为这是不言自明的模板问题 - 无法将函数作为参数传递(无效使用非静态成员函数)
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;
}
我把它从这个功能:
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;
}
}
}
我真的看不出什么毛病。然而,当我编译它时,会发生以下情况:
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
这些有点随机。我看不出这里的静力学是如何发挥作用的。所有被引用的getter都是简单的const返回值。
我很乐意得到一些意见。
C++ FAQ建议[引发临时](http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.12)。 – 2010-11-06 17:04:12