0

I have the following class that I want to serve as an "interface" for other classes. I am using Eigen library.

class MultivariateDistribution
{
public:
    MultivariateDistribution() = default;
    template <typename T>
    MultivariateDistribution(const Matrix<T, Dynamic, Dynamic> &dataset) { maximumLikelihoodParameterEstimation(dataset); }
    virtual ~MultivariateDistribution() = default;

    template <typename T>
    virtual void maximumLikelihoodParameterEstimation(const Matrix<T, Dynamic, Dynamic> &dataset) = 0;
};

I have dervied a class from this one and implemented all the required methods but the compiler yields the following error:

 error C2898: 'void MultivariateDistribution::maximumLikelihoodParameterEstimation(const Eigen::Matrix<T,-1,-1,0,-1,-1> &)': member function templates
cannot be virtual

It is not possible to declare a member function with templated parameters? If so, how can I implement a similar behaviour to receive any type of Eigen Matrix?

Thank you.

Javier
  • 311
  • 5
  • 12
  • No you can't have templated virtual function, how would the vtable look like if you could? – StoryTeller - Unslander Monica Apr 03 '16 at 14:53
  • So in order to implement a similar behaviour, I have to overload the function with all types of Eigen Matrix? I see that Christophe marked as duplicated, If he can post the link of the thread I would appreciatte to see the answers. – Javier Apr 03 '16 at 14:58
  • That depends. Do you really need dynamic dispatch? – StoryTeller - Unslander Monica Apr 03 '16 at 14:59
  • Honestly, I do not know what exactly means dynamic dispatch. I want to allow the users to pass a Matrix of ints, doubles, floats, uchar, etc without forcing them to do a conversion – Javier Apr 03 '16 at 15:00
  • 1
    1. The error message gives a quite precise answer to your question: `member function templates cannot be virtual`. 2. Why do you want to use virtual functions here? They only apply where you want run time polymorphism (i.e. in your case you have multiple different types derived from `MultivariateDistribution` where the selection of one of them is due to user input). – Pixelchemist Apr 03 '16 at 15:02
  • Maybe I am not understanding something... I am not a hard c++ programmer. What I want is to allow the user to pass an Eigen matrix of any type (int, double, float, and so on). I templated the function because I thought it was the easiest and shortest way to do it. I also can overload the function of all types, I don't know if this is the "correct way" to to it. – Javier Apr 03 '16 at 15:04
  • That is the correct way to do this, what I don't understand is why you think it needs to be virtual as well? `MultivariateDistribution` is not something I expect to be a base class of some hierarchy, surely there's only one way to do `maximumLikelihoodParameterEstimation` for a `MultivariateDistribution`. – StoryTeller - Unslander Monica Apr 03 '16 at 15:08
  • Because I want to derive for example a `MultivariateNormal`, a `MultivariateTStudent` and so on. The maximum likelihood implementation for the estimation of the parameters of a specific distribution will be different for each one. An in order to allow to other users to implement their own distributions, I want to provide an "interface" to indicate the user which methods are required to work in the framework. – Javier Apr 03 '16 at 15:12
  • Okay, but surely the implementation is not something your framework won't be aware off until run-time. What you are looking for may be static polymorphism accomplished by the [CRTP](https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern) – StoryTeller - Unslander Monica Apr 03 '16 at 15:18
  • Be aware that all your code will need to be rather template heavy. – StoryTeller - Unslander Monica Apr 03 '16 at 15:19
  • Thank you for the info. I will read it, and if I see that the code is going to become to a hell, I will drop the idea and force the user to pass a Matrix of a specific type. Thank you. How can I vote you? – Javier Apr 03 '16 at 15:24
  • You can't, since I didn't provide an answer. But don't worry about it, I just hope I was able to help. – StoryTeller - Unslander Monica Apr 03 '16 at 15:44

0 Answers0