2011-11-22 4 views
0

다음과 같이 구현 된 클래스가 있습니다. 생성자에서 컴파일 오류가 발생합니다. 내 이유를 말해 줄래?구성원 함수 포인터 오류

class A{ 

public: 
    typedef void (A::*HANDLER)(); 
    void test1(){ 
     printf("This is test 1"); 
    } 
    void test2(){ 
     printf("This is test 2"); 
    } 

    A(){ 
     HANDLER h= &A::test1; 
     h(); // an error spawn here with the description: term does not evaluate to a function taking 0 arguments 
    } 
}; 
당신은 사용해야

답변

0

, 이런 식으로 멤버 연산자 ->* 포인터 : 코드 샘플의

(this->*h)(); 

Online Demo.

0

g++ 오류 메시지가 더 많은 정보이다 : 참으로

bar.cc: In constructor 'A::A()': 
bar.cc:15:11: error: must use '.*' or '->*' to call pointer-to-member function in 'h (...)', e.g. '(... ->* h) (...)' 

, 당신이 (this->*h)();에 전화를 변경하는 경우,이 컴파일러를 전달합니다.