【问题标题】:Boost statechart - compilation error when using state chart as template parameterBoost statechart - 使用状态图作为模板参数时的编译错误
【发布时间】:2016-11-15 11:34:11
【问题描述】:

我正在尝试使用 boost 状态图实现一个简单的状态机。 由于我有这个状态机的几个变体,我认为将它包装在模板中并将状态机作为模板参数传递可能是个好主意。

但是,我遇到了编译错误。

代码:

#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,操作系统:Ubuntu 14.04,升压:1.54)

【问题讨论】:

    标签: c++ templates boost statechart


    【解决方案1】:

    您必须让编译器知道您要调用 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?了解更多信息。

    【讨论】:

    • 现在可以编译了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 2011-04-11
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    相关资源
    最近更新 更多