2011-02-03 60 views
0

編譯STD時,我得到了下面的錯誤:: find_if功能:什麼是錯與此的std :: find_if

error C2451: conditional expression of type 'overloaded-function' is illegal 

的代碼看起來是這樣的:

typedef std::vector<boost::shared_ptr<node> >::iterator nodes_iterator; 

nodes_iterator node_iter = std::find_if(_request->begin(), _request->end(), boost::bind(&RequestValues::is_parameter, this)); 

bool RequestValues::is_parameter(nodes_iterator iter) 
{ 
    return ((*iter)->name.compare("parameter") == 0); 
} 

它似乎有與謂詞函數傳遞給std::find_if有關,但我無法弄清楚什麼是錯誤的,任何人都可以幫忙嗎?

node是一個struct包含一些值。

回答

3

綁定時,您應該使用_1而不是this,並將value_type作爲您的函數的參數。

如果這是一個類或結構成員函數,那麼bind(func, this, _1)也許?但是如果它是一個類成員函數,它可能應該是靜態的,因爲它不需要聲明。

1

您提供給find_if的比較函數不應包含迭代器,而應該包含迭代器指向的值(或者甚至更好,是對其的常量引用)。例如,當在int s範圍內編寫find_if的謂詞時,比較應採用int而不是vector<int>::iterator。將您的比較功能更改爲在shared_ptr<node>上工作可能無法解決所有錯誤,但它至少應占其中的一部分。

1

該函數的簽名應該是

bool RequestValues::is_parameter(boost::shared_ptr<node>); 

即,它並不需要一個迭代,但是迭代器value_type

+0

我仍然得到同樣的錯誤功能簽名.... :( – 2011-02-03 09:06:56