2016-12-03 1 views
0

함수 포인터 목록이있는 추상 클래스가 있는데 그 하위 클래스의 함수 포인터를 삽입하려고합니다. 내가 자식 클래스의 기능을 가지고해야 test1_Engine.cpp:37:32: error: ISO C++ forbids taking the address ofan unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&test1_Engine::f2’ [-fpermissive]하위 클래스의 함수를 추상 부모 클래스의 함수 목록에 추가하십시오.

하지만, 추상 부모 클래스에서 함수의 목록 : 나는 오류가 발생하지만

class Parent { 
    public: 
     typedef Output* (*function_pointer)(Input*); 
     vector<function_pointer> compute_functions; 
} 
class Child : public Parent { 
public: 
     Output* f1(Input* input) {} 
     Output* f2(Input* input) {} 
     void user_insertFunctions(){ 
       compute_functions.push_back(&f1); 
       compute_functions.push_back(&f2); 
     } 
} 

: 나는 다음과 같은 코드가 있습니다. 어떻게해야합니까?

+1

'f1'과'f2'는 함수가 아닙니다. 그것들은 클래스 메소드입니다. 큰 차이. 그것들을 함수로 원한다면'static' 키워드로 선언하십시오. –

답변

0

f1과 f2는 클래스 멤버이므로 입력 매개 변수로 벡터에 전달할 수 없습니다. 다음과 같이 compute_functions 유형을 변경하여 코드를 간단하게 수정할 수 있습니다.

class Input; 
class Output; 
class Child; 

class Parent { 
    public: 
     using function_pointer = Output* (Child::*)(Input*); 
     vector<function_pointer> compute_functions; 
}; 
class Child : public Parent { 
public: 
     Output* f1(Input* input) {} 
     Output* f2(Input* input) {} 
     void user_insertFunctions(){ 
       compute_functions.push_back(&Child::f1); 
       compute_functions.push_back(&Child::f2); 
     } 
}; 
관련 문제