I am trying to implement a FSM class and for making it more generic, I decided to use template classes; however, I am receiving some errors. The errors are not very clear (thanks to Xcode), but I think the problem is the way how I am declaring my classes and handling the inheritance, etc.
UPDATE:
Errors:
Here's my code:
FiniteStateMachine.hpp
---------------------------------
template<class RETURNS, typename PARAMS>
class BaseState
{
public:
// For forcing all the classes have this method
virtual std::vector<RETURNS> performDecision(PARAMS& pList) = 0;
};
template<class R, typename P>
class FSM_State : public BaseState<R, P>
{
public:
FSM_State(int pStateNum = 0);
virtual ~FSM_State();
void init(int pStateNum = 0);
void addState(FSM_State<R,P>* pState);
void addState(const int pIndex, FSM_State<R,P>* pState);
virtual std::vector<R> performDecision(P& pList) = 0;
protected:
std::vector<FSM_State*> mStateList;
};
OHB_DT_FSM.hpp
-----------------------------------------
class OHB_DT_FSM_State : public FSM_State<eDECISION_TYPES, GameAI>
{
public:
OHB_DT_FSM_State(int pStateNum = 0)
: FSM_State(pStateNum)
{}
virtual ~OHB_DT_FSM_State()
{
delete mDecisionTree;
}
virtual void constructDT() = 0;
virtual std::vector<eDECISION_TYPES> performDecision(GameAI& pList) = 0;
protected:
eSTATE_TYPES mType;
std::vector<eDECISION_TYPES> mDecisionList;
DecisionTree* mDecisionTree;
};
This is how I am handling the inheritance, but since I am not experienced about templates, I am not sure about my approach.
For every method that is not described in .hpp file, I have the codes in .cpp files.
CPP File:
template<class R, typename P>
FSM_State<R, P>::FSM_State(int pStateNum)
{
init(pStateNum);
}
template<class R, typename P>
FSM_State<R, P>::~FSM_State()
{
for(int si = 0; si < mStateList.size(); si++)
{
delete mStateList[si];
}
mStateList.clear();
}
template<class R, typename P>
void FSM_State<R, P>::init(int pStateNum)
{
if(pStateNum > 0)
{
mStateList.resize(pStateNum);
}
}
template<class R, typename P>
void FSM_State<R, P>::addState(FSM_State* pState)
{
mStateList.push_back(pState);
}
template<class R, typename P>
void FSM_State<R, P>::addState(const int pIndex, FSM_State* pState)
{
mStateList[pIndex] = pState;
}
Thank you!