2013-04-24 2 views
3

아래 코드를 컴파일하는 동안 다음 오류가 발생합니다. 나는 혼란스럽고 여기서 무엇이 잘못되었는지 알아낼 수 없다. 멤버 함수 포인터의 참조가 잘못 되었습니까?"pos -> * op에서 연산자 -> *가 없습니다."

오류 :

#g++ fp.cpp 
fp.cpp: In member function âvoid Y::callfptr(void (X::*)(int))â: 
fp.cpp:33: error: no match for âoperator->*â in âpos ->* opâ 

fp.cpp

#include <iostream> 
#include <vector> 
using namespace std; 

class B { 
// some base class 
}; 

class X : public B { 
public: 
    int z; 
    void a(int a) { 
    cout << "The value of a is "<< a << endl; 
    } 
    void f(int b) { 
    cout << "The value of b is "<< b << endl; 
    } 
}; 

class Y : public B { 
public: 
    int b; 
    vector<X> vy; 
    void c(void) { 
    cout << "CLASS Y func c called" << endl; 
    } 
    void callfptr(void (X::*op)(int)); 
}; 

void Y::callfptr(void (X::*op) (int)) { 
vector<X>::iterator pos; 
for (pos = vy.begin(); pos != vy.end(); pos++) { 
    (pos->*op) (10); 
} 
} 

답변

4

대신이 일을 :

(pos->*op) (10); 

을이 작업을 수행합니다 :

((*pos).*op)(10); 

반복자는 operator ->*의 과부하를 제공 할 필요가 없습니다.

((pos.operator ->())->*op)(10) 

을하지만 이것은 단지 더 자세한입니다 : 당신이 정말로 operator ->* 대신 operator .*를 사용하려는 경우, 당신은 할 수 있습니다.

차이점은 ->* 연산자는 오버로드 될 수 있지만 operator .* 수 없습니다.

+0

큰 감사

(*pos).*op 
이 사용할 수 있습니다 – user1663533

+0

user1663533 @를 해결 –

2

->*operator ->*입니다. 다행이 도움이 :) : 당신은

pos.operator->()->*op 

하거나

내 문제 너무 좋은 설명 ...