Class 正在寻找boost::msm基本状态机中的多态性示例 有人知道多态boost::msm状态机的示例吗?
在提供的基本示例中,我尝试将转换操作更改为虚拟,但链接器说: 对“vtable for DerivedClass”的未定义引用 编辑: 错误是通过分析代码解决的一个基本且无聊的包含/定义错误 在仔细阅读了基本示例之后,我意识到要使其具有多态性,我应该在子类中定义后端并从前端继承 下面的源文件定义了多态基本msm:Class 正在寻找boost::msm基本状态机中的多态性示例 有人知道多态boost::msm状态机的示例吗?,class,polymorphism,boost-msm,Class,Polymorphism,Boost Msm,在提供的基本示例中,我尝试将转换操作更改为虚拟,但链接器说: 对“vtable for DerivedClass”的未定义引用 编辑: 错误是通过分析代码解决的一个基本且无聊的包含/定义错误 在仔细阅读了基本示例之后,我意识到要使其具有多态性,我应该在子类中定义后端并从前端继承 下面的源文件定义了多态基本msm: 花是没有状态的死物 FlowerMSM是状态机(将MSM行为赋予Flower的一种方式) LiveFlower同时继承和覆盖FlowerMSM的转换操作 LiveFlower.cp
- 花是没有状态的死物
- FlowerMSM是状态机(将MSM行为赋予Flower的一种方式)
- LiveFlower同时继承和覆盖FlowerMSM的转换操作
#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());
}
#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;
}
#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 ";}
#包括“Flower.hpp”
#包括“FlowerMSM.hpp”
类活花:公共花,公共玩家
{
无效水作用(evNoWater const&);
无效停止水作用(平均水流量&);
};
//闲散
void LiveFlower::waterAction(evNoWater const&{std::cout在最初的帖子中,现在提供了答案