【发布时间】:2018-01-18 20:38:30
【问题描述】:
我正在尝试实现一个 FSM 类,为了使其更通用,我决定使用模板类;但是,我收到了一些错误。错误不是很清楚(感谢 Xcode),但我认为问题在于我如何声明我的类和处理继承等。
更新:
错误:
这是我的代码:
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;
};
这就是我处理继承的方式,但由于我对模板没有经验,所以我不确定我的方法。
对于 .hpp 文件中未描述的每种方法,我都有 .cpp 文件中的代码。
CPP 文件:
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;
}
谢谢!
【问题讨论】:
-
您将要重新定义函数是一个 cpp 文件:stackoverflow.com/questions/495021/…
-
有什么错误?你可能不清楚,但其他人可能不清楚
-
函数定义似乎正确,cpp 文件和错误代码已添加@NathanOliver
标签: c++ templates inheritance pure-virtual