2013-06-11 1 views
0

와 find_first_of 기능에 대한 오류를 제공하는 것은 오류를이진 술어 여기

est.cpp (24)을 제공하는 코드

#include <string> 
#include <vector> 
#include <string> 
#include <algorithm> 

using namespace std; 

class test { 
    struct con { 
     string s1; 
     string s2; 
    }; 
public: 
    void somefunc(); 

private: 
    bool pred(con c1, con c2); 
    vector<con> vec; 
}; 

void test::somefunc() 
{ 
    vector<con> v; 
    vector<con>::iterator it = find_first_of(vec.begin(), vec.end(), v.begin(), v.end(), pred); 
} 

bool test::pred(con c1, con c2) 
{ 
    return 0; 
} 

이다 '구조체 시험 :: 죄수 * __ cdecl을 표준 : find_first_of (struct test :: con *, struct test :: con *, struct test :: con *, struct test :: con *, bool (__thiscall *) (struct test :: con, str uct test :: con)) ': bool (struct test :: con, struct test :: con)'에서 'bool (__thiscall *) (struct test :: con, struct test :: con)'매개 변수 5를 변환 할 수 없음 함수 이 범위의 이름이 대상 유형과 일치합니다.

(__siscall *)과 내 조건 자 함수를 변환하는 방법을 이해하지 못합니다.

+2

멤버 함수 포인터는 함수 포인터와 다릅니다. 합리적으로 변환 할 수는 없습니다. – chris

답변

1

__thiscall비 정적 인 멤버 함수는 MSVC++에서만 사용되는 호출 규칙입니다. 코드에서

predstd::bind 일부 해결하지 않고 바이너리 술어로 사용할 수없는 비 정적 멤버 함수입니다. 차라리이 기능을 만들 것을 제안합니다. static. 또는 lamda을 더 잘 사용하십시오 (C++ 11에만 해당).

2

술어는 비 정적 멤버 함수가 될 수 없습니다. 이것은 암시 적 첫 번째 매개 변수를 취하여 총 세 개의 매개 변수를 제공하기 때문에 비 정적 멤버 함수가 될 수 없습니다.

using namespace std::placeholders; 
vector<con>::iterator it = find_first_of(vec.begin(), vec.end(), 
             v.begin(), v.end(), 
             std::bind(&test::pred, this, _1, _2)); 
std::bind에 전화가와와 호출 엔티티를 생성

: 또는 첫 번째 매개 변수로 this 바인딩 std::bind를 사용

// non-member function 
bool pred(const con& c1, const con& c2) 
{ 
    return false; 
} 

void test::somefunc() 
{ 
    vector<con> v; 
    vector<con>::iterator it = find_first_of(vec.begin(), vec.end(), 
              v.begin(), v.end(), 
              pred); 
} 

: 당신은 정적 멤버 함수 또는 비회원 중 하나가 필요합니다 두 개의 con 매개 변수. 내부에 this 포인터의 사본을 저장합니다 (단 함수에서는 this이 사용되지 않습니다).

0

조건자는 비 정적 구성원 기능입니다. 어느 쪽이 정적 (또는 단순히 비 멤버 함수) 만들거나 std::bind()를 사용 (또는 동등한 Boost.Bind 당신은 C++ (11)를 감당할 수없는 경우) :

여기
#include <functional> 

// ... 

vector<con>::iterator it = find_first_of(
    vec.begin(), vec.end(), v.begin(), v.end(), 
    std::bind(&test::pred, this, std::placeholders::_1, std::placeholders::_2)); 

live example이다. 호출 해 객체의 상태를 변경 안가능성이 있기 때문에

당신이 멤버 함수로 기능을 유지하고 std::bind()를 사용하도록 선택하는 경우

const로 기능 pred() 자격 고려한다.