2016-11-15 9 views
0

부스트 상태 차트를 사용하여 간단한 상태 시스템을 구현하려고합니다. 이 상태 시스템에는 여러 가지 변형이 있기 때문에이를 템플릿에 넣고 상태 시스템을 템플릿 매개 변수로 전달하는 것이 좋습니다.부스트 상태 차트 - 상태 매개 변수를 템플릿 매개 변수로 사용할 때의 컴파일 오류

그러나 컴파일 오류가 발생합니다.

코드 :

#include <boost/statechart/state_machine.hpp> 
#include <boost/statechart/simple_state.hpp> 
#include <boost/statechart/transition.hpp> 

namespace sc = boost::statechart; 


class ComponentType 
{ 
}; 

class FSM { 
protected: 
    struct stInit ;  
public: 
    struct Machine : sc::state_machine< Machine, stInit > {}; 
protected: 

    struct stInit : ComponentType, sc::simple_state< stInit, Machine > {}; 
}; 

template <class fsm> 
void run() { 
    typename fsm::Machine m_fsm; 
    const ComponentType &t = m_fsm.state_cast<const ComponentType &>(); 
    (void) t; 
} 

int main() { 
    run<FSM>(); 
} 

컴파일 오류 : 그러나

fsmtest.cpp: In function ‘void run()’: 
fsmtest.cpp:33:45: error: expected primary-expression before ‘const’ 
    const ComponentType &t = m_fsm.state_cast<const ComponentType &>(); 
              ^
fsmtest.cpp:33:45: error: expected ‘,’ or ‘;’ before ‘const’ 

, 템플릿 대신 형식 정의 사용하는 경우 :

typedef FSM fsm; 
//template <class fsm> 

run(); 
    // run<FSM>(); 

모든 것이 오류없이 컴파일됩니다.

무엇이 누락 되었습니까?

(: OS g ++ 4.8.4 : 컴파일러 우분투 14.04, 부스트 : 1.54)

답변

2

당신은 컴파일러가 당신이 state_cast 템플릿 함수를 호출 할 것인지 알 수 있도록, 그래서 그것은 정확하게 라인을 구문 분석합니다. 변경 :

const ComponentType &t = m_fsm.state_cast<const ComponentType &>(); 

에 자세한 정보를 원하시면

const ComponentType &t = m_fsm.template state_cast<const ComponentType &>(); 

확인 Where and why do I have to put the "template" and "typename" keywords?.

+0

이제 컴파일됩니다. 감사! – oferlivny