代码之家  ›  专栏  ›  技术社区  ›  codentary

Boost流程图状态机状态上下文为空

  •  0
  • codentary  · 技术社区  · 6 年前

    我有一个小房间 sm 只有一个 state 现在。
    状态 ,以获取一些 data 由接收和存储的 山猫

    struct data {
        std::string m_ip;
        data(const char* ip)
            : m_ip(ip)
        {
        }
    };
    
    namespace sc = boost::statechart;
    
    struct s1;
    struct sm : sc::state_machine<sm, s1> {
        data* m_data;
        sm(data* d)
            : m_data { d }
        {
        }
    };
    
    struct s1 : sc::simple_state<s1, sm> {
        s1()
        {
            std::cout << context<sm>().m_data->m_ip; // assertion
        }
    };
    
    int main()
    {
        data _data("192.168.1.1");
        sm _sm(&_data);
        _sm.initiate();
    
        return 0;
    }
    

    test: /usr/include/boost/statechart/simple_state.hpp:682: static OtherContext& boost::statechart::simple_state<MostDerived, Context, InnerInitial, historyMode>::context_impl_other_context::context_impl(State&) [with OtherContext = sm; State = boost::statechart::simple_state<s1, sm>; MostDerived = s1; Context = sm; InnerInitial = boost::mpl::list<mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>; boost::statechart::history_mode historyMode = (boost::statechart::history_mode)0]: Assertion `get_pointer( stt.pContext_ ) != 0' failed.
    

    null

    0 回复  |  直到 6 年前
        1
  •  1
  •   codentary    6 年前

    我找到了答案 here

    // This assert fails when an attempt is made to access an outer 
    // context from a constructor of a state that is *not* a subtype of
    // state<>. To correct this, derive from state<> instead of
    // simple_state<>.
    

    不确定是否应将其标记为重复,因为这是同一问题,但触发方式略有不同( context 是一个 outer state state machine