I want to implement an algorithm as a class deriving from a pure virtual class representing the kind of problem the particular algorithm solves.
The general interface would look like this:
template<typename A, typename B>
class ISolutionToProblem
{
public:
virtual void Init(const A & input, const B & param) = 0;
virtual const B & ComputeSolution() = 0;
virtual ~ISolutionToProblem() {}
};
And the implementation would be for example:
template<typename T>
class MyAlgorithm:
public ISolutionToProblem<typename MyAlgorithm<T>::WorkData, T>
{
public:
struct WorkData { /* Stuff using T... */ };
virtual void Init(const WorkData & input, const T & param);
virtual const T & ComputeSolution();
virtual ~MyAlgorithm();
};
(to be more specific, the problem is actually path finding, but I don't think it is relevant)
My problem is the inheritance part: I am using a nested struct as a template parameter, and no matter how nicely I try to talk to the compiler, it keeps refusing to compile my code.
I could go lazy and just put the inner structure outside of the class, but if possible I'd prefer it to stay neatly placed in the class.
- So is what I am trying to do actually possible (in C++98)?
- If so, how should I write it ? (bonus points if you get me to understand why the syntax doesn't accept the form above)
- Otherwise, what am I doing wrong? (is my design flawed to begin with?)
Here is how the compiler error looks like.
- g++ (4.8):
error: no type named ‘WorkData’ in ‘class MyAlgorithm<int>’
- clang (3.1):
error: no type named 'WorkData' in 'MyAlgorithm<T>'
- VS2012:
error C2146: syntax error : missing ',' before identifier 'WorkData' see reference to class template instantiation 'MyAlgorithm<T>' being compiled error C2065: 'WorkData' : undeclared identifier error C2955: 'ISolutionToProblem' : use of class template requires template argument list see declaration of 'ISolutionToProblem'
Data
supposed to be? – AvuncularT ComputeSolution()
, it would bebool ComputeStepsTowardSolution(int maximumSteps)
andT GetSolution()
. – Connel