首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boost statechart‘`state’为空

Boost statechart‘`state’为空
EN

Stack Overflow用户
提问于 2019-10-21 06:21:22
回答 1查看 226关注 0票数 2

我现在有一个小sm,只有一个state

我希望从state内部访问由sm在构建时接收和存储的一些data

代码语言:javascript
复制
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;
}

当我运行这个程序时,我会得到以下错误:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-21 07:55:25

我找到了答案,here

它正盯着我的脸,从上面的评论中可以看出:

代码语言:javascript
复制
// 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本身,就像我的例子一样)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58480752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档