I don't see any template, even more I haven't use them in C++. even
that way, I can
see interfaces and classes, and they have a lit problem:
take out "public" from the method definition in the interface, to set
public there is invalid.
also, put "public" to the interface, because it is less accesible than
the class implenting it, or else, take out public of the class (by
default they are internal, that's package level).
The interface now looks like this:
public interface IBasisFunction1D
{
double Interpolate(double x, IList<double> xPoints,
IList<double> yPoints);
}
Note that if you are writing a lib, and you has set the interface to
public, that means that clients of your lib will be able to declare
new types that implement that interface, if you don't want that,
change it to internal, but you will need to change Interpolator to
internal too.
Ok, by member functions, I think you mean a method, and by template I
think you mean Interface, if so... the main problem is that you are
calling like this:
Method.Interpolate(x, xPoints, yPoints);
There "Method" is the name of the interface, and interfaces doesn't
have static members, that means you
need an object to be able to call "Interpolate" (that you know is not
static anyway).
We have generics, so first, change:
public class Interpolator<Method>
where Method : IBasisFunction1D
to:
public class Interpolator<Method>
where Method : IBasisFunction1D, new()
That's a generic constrain that tells C# that the type Method must
have a public parameterless constructor that you will use (if you
write none, the default one works just fine).
That's just part of the problem, now we can create objects of type
Method let's create one to call "Interpolate" and replace the invalid
static call, so change:
Method.Interpolate(x, xPoints, yPoints);
to:
(new Method()).Interpolate(x, xPoints, yPoints);
Solved!!! but I has another suggestion:
Cache that object, so no need to initialize a new one for every call.
Including that, the final code (that I hope you understand, because if
not, you will run in this problem again), is the following:
public class Interpolator<Method> where Method : IBasisFunction1D,
new()
{
private IBasisFunction1D cache; //this is a private field (a
class level variable)
public double Interp(double x, IList<double> xPoints,
IList<double> yPoints)
{
//If cache is null (that's the default for classes) assign
to it a new object of type Method (whatever it is)
if (cache == null) cache = new Method();
return cache.Interpolate(x, xPoints, yPoints); //Call with
cache, that must has been initialized now
//Note: if Method is a struct, it will never be null, but
also you will not need to initialize it, so no problem.
}//If you did copy -> paste, this is the proof of if, so easy
this, but make sure of what this code does.
}
... and so on ...
you see? once understood, doing this in C# is trival.
Don't get drown in a glass with water.
~Theraot