【问题标题】:Boost statechart `state context` is nullBoost状态图`状态上下文`为空
【发布时间】:2019-10-21 06:21:22
【问题描述】:

我有一个小的sm,现在只有一个state
我想从state 中访问一些data 在构造时由sm 接收和存储:

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

【问题讨论】:

    标签: c++ boost state-machine statechart


    【解决方案1】:

    我找到了答案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<>.
    

    不确定是否应该将其标记为重复,因为它是同一个问题,但触发方式略有不同(contextouter state,而不是 state machine 本身,在我的情况下)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2011-05-29
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多