c++ - Boost 状态图 `state context` 为空

标签 c++ boost state-machine statechart

我有一个小的 sm,目前只有一个 状态
我想从状态内部访问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

最佳答案

我找到了答案 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 本身,如我的情况)。

关于c++ - Boost 状态图 `state context` 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58480752/

相关文章:

c++ - 从 boost::property_tree 读取数组出现空白

c# - 如何持久化实现状态模式的对象?

c++ - 具有高维标签的机器学习代码 dlib

C++ Vector 给出难以置信的意外值(value)

c++ - 振奋 spirit 2 : is there a way to know what is the parser progression percentage?

c++ - 使用 BOOST::associative property map 插入 boost::BIMAP ... 失败

regex - 从左到右阅读时,a 和 b 的数量相同,但一个字母的数量不得超过另一个字母的三个以上

ruby-on-rails - 如何为状态机或有限自动机实现 RESTful 资源

c++ - 从 Windows 系统菜单中删除“移动”和“关闭”命令,而不会丢失功能

C++17:在编译时将类型映射到整数值