2010-11-06 49 views
0

這是我製作的模板。我認爲這是不言自明的模板問題 - 無法將函數作爲參數傳遞(無效使用非靜態成員函數)

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返回值。

我很樂意得到一些意見。

回答

2

if (((*it)->*f)() == element)

0

它已經有一段時間,但是否缺少新的?

throw new NoItemsFound<I>(element); 
+2

C++ FAQ建議[引發臨時](http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.12)。 – 2010-11-06 17:04:12

1

的規範的方法來複制的元件是使用否定std::remove_copy_if()或寫信copy_if()算法。恕我直言copy_if()是更清晰的選擇,並且儘管它已經不是一個標準功能,它很容易正確地寫:

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

然後,您可以使用它,像這樣:

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) { // ... 

的好處在這裏你可以使用copy_if()做很多其他的事情。它適用於任何容器,而不僅僅是std::list<N*>,它可以與任何一元謂詞一起使用,無論是原始函數還是函數對象。

+0

感謝您的額外輸入!我將爲最終的下一個項目考慮這一點,但現在重構所有功能的成本太高。 – 2010-11-06 17:47:49

相關問題