2016-11-15 7 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を使用している場合:

typedef FSM fsm; 
//template <class fsm> 

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

すべてがエラーなくコンパイルされます。

私には何が欠けていますか?

(コンパイラ:G ++ 4.8.4、OS:Ubuntuの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

関連する問題