I am using a template in a header file. The function I am using is recursive and I would like to keep two variables outside of the function to compare against. Here is my code (note: does not compile):
#include "Position.h"
#ifndef _SOLVER_H
#define _SOLVER_H
class Solver{
public:
template<typename T>
int EvaluatePosition(T &p, bool me) {
int score = 0;
if(p.isGameOver()) {
return p.score(me);
}
else {
std::vector<T> nextMoves = p.getNextPositions();
for(int i=0; i != nextMoves.size(); i++) {
score += EvaluatePosition(nextMoves[i],!me);
if(score > maxScore) {
p.display();
maxScore = score;
maxP = p;
p.display();
}
}
return score;
}
}
T maxP; // Want this to be the same Type T that is used in the function
int maxScore;
};
#endif
I am trying to create a variable of the same generic type as the T used in the function so that I can save out some data. Is this possible, and if it is, how would it be done?
You can make your whole class a templated one, not just the function.