【发布时间】:2021-02-24 16:04:55
【问题描述】:
我正在尝试在主状态机和子状态机之间共享SyncBox 对象。理想的方法是将它传递给构造函数(在更复杂的情况下,子状态机将是区域之一的初始状态)。无论如何,我无法使这段代码正确编译和执行。有什么帮助吗?
#include <iostream>
#include <mutex>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/euml/common.hpp>
namespace msm = boost::msm;
namespace mpl = boost::mpl;
using namespace boost::msm::front;
class SyncBox {
std::mutex mtx;
};
struct SubSMFE : public msm::front::state_machine_def<SubSMFE>
{
struct InitState : public msm::front::state<> {};
struct FakeState : public msm::front::state<> {};
typedef InitState initial_state;
struct transition_table : mpl::vector<
Row < InitState, none, FakeState>
> {};
};
//typedef msm::back::state_machine<SubSMFE> SubSM;
class SubSM : public msm::back::state_machine<SubSMFE> {
private:
SyncBox& sb;
public:
SubSM(SyncBox& sb) : sb(sb) { std::cout << "SubSMFE constructor" << std::endl; };
void oneFunction() {
// here i use syncBox. it must be a function of SubSM, not SubSMFE (oneFunction overrides start or enqueue_event)
};
};
struct mainSMFE : public msm::front::state_machine_def<mainSMFE>
{
protected:
SyncBox sb;
public:
struct InitState : public msm::front::state<> {};
typedef InitState initial_state;
struct transition_table : mpl::vector<
Row < InitState, none, SubSM>
> {};
};
class mainSM : public msm::back::state_machine<mainSMFE> {
public:
mainSM() : msm::back::state_machine<mainSMFE>(msm::back::states_ << SubSM(sb)) { };
};
int main()
{
mainSM sm;
return 0;
}
【问题讨论】:
-
您的所有基类都有正确的模板参数吗?对我来说看起来像是 CRTP 类型的东西,因此,例如,期望
class SubSM将自己作为模板参数传递给基类 -
@wreckgar23 不确定你的意思
-
@wreckgar23 对我来说看起来不错。它是一个 CRTP,但是是分层的(从后端 defs 派生的前端机器)
-
@AndrewBloom 我说的是模板参数被传递给你从中派生状态机的类。在本地编译时,
transition_table抱怨SubSM不是默认可构造的 -
我得到“错误 C2668 'boost::msm::back::state_machine
::state_machine': 在 Visual Studio 2019 和 boost 1.75.0 上的 MainSM() 定义中对重载函数的模糊调用,但我尝试了很多变体并遇到了像你这样的其他一些错误。