Original Post
Hi, For my first real foray into the magical, wonderful world of Templates, I'm trying to create a policy-driven A* pathfinding class, and have hit a problem. Wonder if anyone can help. I have the following definition: I was using a dummy EndEvaluator class, but have just added a constructor and variable, as follows: The class is called by: Before I added the parameterised constructor to the template call, everything worked fine. Now I have SingleEndNode taking a parameter, it's all gone pear-shaped - I get the following error messages: c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\Templated PathFinding\Main.cpp(15): error C2974: 'AStarPathFinder' : invalid template argument for 'EndEvaluator', type expected c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\Templated PathFinding\Main.cpp(15): error C2955: 'AStarPathFinder' : use of class template requires template argument list c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\Templated PathFinding\Main.cpp(15): error C2133: 'pathFinder' : unknown size What am I doing wrong? Do I need to construct a SingleEndPoint instance prior to calling the template? Is there any way to get around this? What's going on? I assume this is happening because I'm now trying to derive from an instance instead of from a class - if so, how do I get around this? Thanks in advance for any help, Jim. Edit : OK - so the issue is that I'm trying to inherit from an instance, not a class. But if I want to have the option of 2 different EndEvaluators - the SingleEndNode as above and a MultipleEndNode which contains a vector of MapNode pointers - how do I write the ctor for the PathFinder class? In one case it needs a ctor with a MapNode*, in the other it needs a ctor with a vector of MapNode*'s. [Edited by - JimPrice on April 8, 2005 7:55:22 PM]
template
<
class EndEvaluator,
class Heuristic,
class Map,
class NodeType
>
class AStarPathFinder : public EndEvaluator, public Heuristic, public Map
{
public:
// Actually runs the pathfinding
void FindPath(const Vector2& start, std::vector<Vector2>& actionStack);
private:
// A wrapper class used to store the pathfinding information
struct PathFindNode
{
PathFindNode(const NodeType* const node, PathFindNode* parent, float G, float H) :
node_(node), parent_(parent), F_(G + H), G_(G), H_(H) {}
const NodeType* const node_;
PathFindNode* parent_;
float F_;
float G_;
float H_;
bool operator==(const PathFindNode& other)
{return node_ == other.node_;}
bool operator==(const NodeType* const other)
{return node_ == other;}
};
};
class SingleEndNode
{
public:
SingleEndNode(const MapNode* const destination) : destination_(destination) {}
protected:
bool AtEnd(const MapNode* const test);
private:
const MapNode* const destination_;
};
MapNode* mapNode = new MapNode;
std::vector<Vector2> actions;
// Create the pathfinder
AStarPathFinder<SingleEndNode(mapNode), ManhattanDistance, Map, MapNode> pathFinder;
// Run the pathfinder
pathFinder.FindPath(Vector2(0, 0), actions);