简体   繁体   English

Boost状态图-使用状态图作为模板参数时出现编译错误

[英]Boost statechart - compilation error when using state chart as template parameter

I'm trying to implement a simple state machine using boost statechart. 我正在尝试使用boost状态图实现一个简单的状态机。 Since I have several variation of this state machine, I thought it might be a good idea to wrap it in a template and pass the state machine as template parameter. 由于该状态机有多种变体,因此我认为将其包装在模板中并将状态机作为模板参数传递可能是一个好主意。

However, I'm getting compilation errors. 但是,我遇到了编译错误。

Code: 码:

#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>();
}

The compilation errors: 编译错误:

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’

However, when using typedef instead of template: 但是,当使用typedef而不是模板时:

typedef FSM fsm;
//template <class fsm>

and

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

Everything compiles without any errors. 一切都能编译,没有任何错误。

What am I missing? 我想念什么?

(compiler: g++ 4.8.4, OS: Ubuntu 14.04, boost: 1.54) (编译器:g ++ 4.8.4,操作系统:Ubuntu 14.04,boost:1.54)

You have to let compiler know that you want to call state_cast template function, so it would parse the line correctly. 您必须让编译器知道您要调用state_cast模板函数,因此它将正确解析该行。 Change: 更改:

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

to: 至:

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

Check Where and why do I have to put the "template" and "typename" keywords? 检查在何处以及为什么必须将“ template”和“ typename”关键字放入? for more info. 有关更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM