0

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

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

未定义对“DerivedClass 的 vtable”的引用

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

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

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

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

活花.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());
}

花MSM.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 ";}

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

4

1 回答 1

0

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

于 2013-09-03T06:03:22.417 回答