【问题标题】:looking for examples of polymorphism in boost::msm basic state machine在 boost::msm 基本状态机中寻找多态性的例子
【发布时间】:2013-08-31 11:47:45
【问题描述】:

我是 C++ 新手,我是 boost 和使用状态机的新手。 有人知道多态 boost::msm 状态机的例子吗?

我尝试在提供的基本示例中将转换操作更改为虚拟,但链接器显示:

对 `vtable for DerivedClass' 的未定义引用

编辑: 错误是一个基本且无聊的包含/定义错误,通过分析代码已解决。

再次仔细阅读基础示例后,我意识到要使其具有多态性,我应该在子类中定义后端并从前端继承。

以下源文件定义了一个多态基本 msm:

  • 花是没有状态的死物
  • FlowerMSM 是状态机(一种将 MSM 行为赋予 Flower 的方法)
  • LiveFlower 继承自 FlowerMSM 并覆盖了转换操作

LiveFlower.cpp

#include "Flower.hpp"
#include "FlowerMSM.hpp"

class LiveFlower: public Flower, public player_ 
{
        void waterAction(evNoWater const&);
        void stopWateringAction(evEnoughWater const&);
};
//Idle
void LiveFlower::waterAction(evNoWater const&)       { std::cout << "Here goes call to LiveFlower::waterAction"; }
//Watering
void LiveFlower::stopWateringAction(evEnoughWater const&)       { std::cout << "Here goes call to LiveFlower::stopWateringAction"; }

typedef msm::back::state_machine<LiveFlower> FonFon;

int main()
{
    FonFon =new FonFon f();
    f.start();
    f.process_event(evNoWater());
}

FlowerMSM.cpp

#include <iostream>
// back-end
#include <boost/msm/back/state_machine.hpp>
//front-end
#include <boost/msm/front/state_machine_def.hpp>
// FlowerMSM is the boost::msm state machine of Flower and defines the logic of its dynamic behavior in response to external events.
namespace msm = boost::msm;
namespace mpl = boost::mpl;


namespace
{

    // events

    struct evNoWater {};
    struct evEnoughWater {};
    // front-end: define the FSM structure 
    struct player_ : public msm::front::state_machine_def<player_>
    {
        template <class Event,class FSM>
        void on_entry(Event const& ,FSM&) 
        {
            std::cout << "entering: Flower Player" << std::endl;
        }
        template <class Event,class FSM>
        void on_exit(Event const&,FSM& ) 
        {
            std::cout << "leaving: Flower Player" << std::endl;
        }

// The list of Flower states
        struct Idle : public msm::front::state<> 
        {
            // every (optional) entry/exit methods get the event passed.
            template <class Event,class FSM>
            void on_entry(Event const&,FSM& ) {std::cout << "Flower entering: Idle" << std::endl;}
            template <class Event,class FSM>
            void on_exit(Event const&,FSM& ) {std::cout << "Flower leaving: Idle" << std::endl;}
        };

        struct Watering : public msm::front::state<> 
        {
            // every (optional) entry/exit methods get the event passed.
            template <class Event,class FSM>
            void on_entry(Event const&,FSM& ) {std::cout << "Flower entering: Watering" << std::endl;}
            template <class Event,class FSM>
            void on_exit(Event const&,FSM& ) {std::cout << "Flower leaving: Watering" << std::endl;}
        };

        // the initial state of the Flower player SM. Must be defined
        typedef Idle initial_state;
        // transition actions

    //Idle
        virtual void waterAction(evNoWater const&)       { std::cout << "Here goes call to Flower::waterAction"; }


    //Watering
        virtual void stopWateringAction(evEnoughWater const&)       { std::cout << "Here goes call to Flower::stopWateringAction"; }

        typedef player_ p; // makes transition table cleaner

        // Transition table for player
        struct transition_table : mpl::vector<
            //    Start     Event         Next      Action               Guard
            //  +---------+-------------+---------+---------------------+----------------------+

          a_row < Idle , evNoWater        , Watering , &p::waterAction                      >,
          a_row < Watering , evEnoughWater        , Idle , &p::stopWateringAction                      >
            //  +---------+-------------+---------+---------------------+----------------------+
        > {};
        // Replaces the default no-transition response.
        template <class FSM,class Event>
        void no_transition(Event const& e, FSM&,int state)
        {
            std::cout << "no transition from state " << state
                << " on event " << typeid(e).name() << std::endl;
        }


    };
    // Pick a back-end
    typedef msm::back::state_machine<player_> FlowerMSM;
}

为了完整起见,“死”花类: Flower.hpp

#include <string>
#include <iostream>

class Flower
{
    public:
        Flower();
        Flower(std::string type);
        std::string getType();
        void setType(std::string type);
        void water();
        void stopWatering();
    private:
        std::string typ;
};
Flower::Flower(){}

Flower::Flower(std::string type){typ=type;}
std::string Flower::getType(){return typ;}
void Flower::setType(std::string type){typ=type;}
void Flower::water(){std::cout<<"watering now";}
void Flower::stopWatering(){std::cout<<"stopped watering ";}

编辑: 不接受我自己的答案,以防有人提出更好的解决方案。

【问题讨论】:

    标签: class polymorphism boost-msm


    【解决方案1】:

    现在在原始帖子中提供了答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-22
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多