0
我正在嘗試爲我的程序製作一個決策樹,其中每個節點都具有類似的分支功能,但在分支方法中具有不同的主體。我已經想出瞭如何讓函數指針進入方法,但現在它似乎不想將指針指向類之外的方法。將函數指針傳遞給構造函數C++
這是我的節點類
class FighterDecisionTreeNode :
public IDecisionTreeNode
{
private:
FighterDecisionTreeNode* posChild;
FighterDecisionTreeNode* negChild;
DecisionFunction *decisionFunc;
public:
FighterDecisionTreeNode(DecisionFunction *decisionFunction);
~FighterDecisionTreeNode(void);
FighterDecisionTreeNode* getChild(bool child);
bool getBranch(SpaceObject* object, SpaceObject* thisShip);
IDecisionTreeNode* makeDecision(SpaceObject* object, SpaceObject* thisShip);
};
FighterDecisionTreeNode::FighterDecisionTreeNode(DecisionFunction * decisionFunction)
{
decisionFunc = decisionFunction;
}
FighterDecisionTreeNode::~FighterDecisionTreeNode(void)
{
}
FighterDecisionTreeNode* FighterDecisionTreeNode::getChild(bool child)
{
if(child)
{
return posChild;
}
else
{
return negChild;
}
}
IDecisionTreeNode* FighterDecisionTreeNode::makeDecision(SpaceObject* object, SpaceObject* thisShip)
{
return getChild(getBranch(object, thisShip))->makeDecision(object,thisShip);
}
bool FighterDecisionTreeNode::getBranch(SpaceObject* object, SpaceObject* thisShip)
{
return decisionFunc(object, thisShip);
}
這是我的樹類
class FighterDecisionTree:
public IDecisionTree
{
private:
//decision methods
bool isEnemy(SpaceObject* enemy, SpaceObject* ship);
//decision nodes
FighterDecisionTreeNode * isEnemyNode;
public:
FighterDecisionTree(void);
virtual ~FighterDecisionTree(void);
bool getBranch(SpaceObject* object);
virtual IDecisionTreeNode* makeDecision();
};
FighterDecisionTree::FighterDecisionTree(void)
{
isEnemyNode = new FighterDecisionTreeNode(&isEnemy);
}
FighterDecisionTree::~FighterDecisionTree(void)
{
}
IDecisionTreeNode* FighterDecisionTree::makeDecision(){
return NULL;
}
bool FighterDecisionTree::getBranch(SpaceObject* object)
{
return false;
}
// I need to pass a condition to each seperate node,
//but each must have a seperate condition
bool FighterDecisionTree::isEnemy(SpaceObject* enemy, SpaceObject* ship)
{
return true;
}
這是節點類使用
class IDecisionTreeNode
{
private:
//This is for creating the BST
//IDecisionTreeNode* posChild;
//IDecisionTreeNode* negChild;
public:
typedef bool (DecisionFunction)(SpaceObject* enemy, SpaceObject* ship);
IDecisionTreeNode(void);
IDecisionTreeNode(DecisionFunction *decisionFunction);
~IDecisionTreeNode(void);
virtual IDecisionTreeNode* makeDecision(SpaceObject* object, SpaceObject* thisShip)=0;
};
接口
謝謝
請在每行代碼的開頭添加一個額外的4個縮進空格,以便它顯示正確地進行。 – 2010-12-03 22:18:34
請正確縮進您的代碼。渺茫的是,任何人都願意閱讀這樣的代碼(不,我不是在談論每行4個額外空間)。 – jwueller 2010-12-03 22:20:38