2009-05-27 29 views
1

我試圖建立下面的代碼塊在1文件.cpp文件:find_if功能建設問題

#include <iostream> 

#include <algorithm> 
using namespace std; 

class test 
{ 

public: 

    int a[10]; 
    int index; 

    test(); 
    ~test(); 
    bool equals(int p); 
    void search(); 
}; 

test::test() 
{ 
    int temp[10] = {4, 9, 5, 6, 9, 10, 9, 255, 60, 0}; 

    memcpy(a, temp, sizeof(temp)); 

    index = -1; 
} 

bool test::equals(int p) 
{ 
    return p == 9; 
} 

void test::search() 
{ 
    int* p = std::find_if(a, a+10, &test::equals); 
    while (p != a+10) 
    { 
     cout<< *p; 
     index = p - a; 
     p = std::find_if(p+1, a+10, &test::equals); 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    test object; 

    object.search(); 

    return 0; 
} 

我得到一個錯誤,如下圖所示,我不知道到底當我在類的成員方法中使用find_if函數時發生了什麼,並且每當我這樣做時都會收到此錯誤。

 
1>c:\program files\microsoft visual studio 8\vc\include\algorithm(87) : error C2064: term does not evaluate to a function taking 1 arguments 
1>  c:\program files\microsoft visual studio 8\vc\include\algorithm(96) : see reference to function template instantiation '_InIt std::_Find_if(_InIt,_InIt,_Pr)' being compiled 
1>  with 
1>  [ 
1>   _InIt=int *, 
1>   _Pr=bool (__thiscall test::*)(int) 
1>  ] 
1>  c:\testprogram\nomfc\main.cpp(32) : see reference to function template instantiation '_InIt std::find_if(_InIt,_InIt,_Pr)' being compiled 
1>  with 
1>  [ 
1>   _InIt=int *, 
1>   _Pr=bool (__thiscall test::*)(int) 
1>  ] 
+0

若要創建代碼塊,請縮進4個空格,或按下1和0的按鈕 – 2009-05-27 00:21:22

回答

1

find_if函數需要一個可調用的對象,它不帶任何參數。這就像一個自由函數,一個函數對象或一個靜態類函數。你傳入了equals成員函數的地址,這些都不是。您可以通過使equals函數成爲自由函數或靜態函數來解決此問題,因爲它不需要test實例的任何成員。

// static 
class test 
{ 
    public: 
    static bool equals(int p); // etc 
}; 
int* p = std::find_if(a, a+10, &test::equals); 

// free 
bool equals(int p) 
{ 
    return p == 9; 
} 
int* p = std::find_if(a, a+10, equals); 

如果你真正的代碼示例要求,這是一個成員函數,那麼你需要在充當封閉在類的實例函數對象通過。我贊成使用Boost綁定方法,但也有其他方法。

int* p = std::find_if(a, a+10, boost::bind(&test::equals, this, _1)); 
+0

類定義中的equals()函數聲明是否包含「int p」作爲參數? – stanigator 2009-05-27 05:31:58

1

test::equals是一個成員函數,它具有與普通函數指針不同的指針語法。特別是,要調用它,find_if將需要一個test類型的對象,它不具有(例如,它不會自動將它稱爲this,我猜你正在考慮這個對象)。

您可以將功能equals移動到類test之外,它應該可以工作。

0

的第三個參數find_if必須是(指針)函數或仿函數取一個參數,而不是(指針的)實例方法,它是你使用的是什麼。例如,一個合適的函子可能(鬆散從[此線索]改編[1]):

template <typename PType, typename ArgType> 
class is_good_test : unary_function<PType, bool> 
{ public: 
is_good_test(const ArgType & arg) : _val(arg) { } 
~is_good_test() { } 

bool operator()(const PType p) const 
{ 
return p->equals(_val); 
} 

private: 
ArgType _val; 
}; 

,它可以讓你通話,如:

std::find_if(a, a+10, is_good_test<test*, int>(10)) 


    [1]: http://www.velocityreviews.com/forums/t288980-functors-and-stl-findif.html 
+1

它看起來不太正確 - 有時你有「val」,其他時候有「_val」,那麼你在find_if行中有模板實例化的錯誤括號類型。 – 2009-05-27 00:36:44

+0

tx,好點子 - 編輯修復錯別字。 – 2009-05-27 00:40:06

1
int *p = find_if(a, a+10, bind1st(mem_fun(&test::equals), this)); 

或者更好的是,獲得擺脫test::equals()成員函數,然後

int *p = find_if(a, a+10, bind2nd(equals(), 9)); 

其中等於實際上std::equals(),二進制函數定義在頭文件<functional>中。

+0

+1正確答案IMO。不需要升壓。 – 2009-05-27 08:15:57

+0

+1與我的情況相關。 – 2012-03-27 21:14:03