1

I got small problem with my study's problem.

    template<typename T> 
    class AlgorytmSortujacy 
       { 
       public: 
          template <typename F> 
          virtual std::vector<T> sortuj(std::vector<T>  w, F porownywacz) const = 0;
       }; 

This code has to be interface for sort algorithms. When I e.x. implement bubble sort, I have to derive from this class and implement the sortuj funtion.

The problem is that VS2013 does not accept those form of code, I mean template virtual function (C2898 error). Do you know of any solutions?

As you see, sort function takes container from std::vector and F porownywacz - it's and functional object which compares two elements of an array

At the end - I think I can't change this code, I got it from the teacher and I think I have to make it work.

Niall
  • 30,036
  • 10
  • 99
  • 142
Maegpaj
  • 9
  • 3

1 Answers1

1

The problem is with F porownywacz, it can't be a template with the pure virtual function.

Virtual member functions can't be templates, to quote clang "virtual cannot be specified on member function templates".

From the cppreference site;

A member function template cannot be virtual, and a member function template in a derived class cannot override a virtual member function from the base class.

The distinction here is essentially ascribed to the virtual functions are a "runtime thing," they are resolved at runtime. The template types are required to be resolved at compile time.

Are you using a conformant complier in the class, what is the teacher using? I would approach you teacher about the issue, quoting the compiler error and to check you are on the same page as your class mates, talk them as well on what error they are getting.


This Q&A contains more detail and some alternatives you may be interested in.

Community
  • 1
  • 1
Niall
  • 30,036
  • 10
  • 99
  • 142
  • We use any compiler we want, for ex. I use VS 2013. So for this moment i don't know what to do, I don't know who is stupid :) – Maegpaj Jan 08 '16 at 07:38
  • What compiler is the teacher using? It sounds like you will need to change the code as it stands and look for an alternative method. I would first just ask the teacher about the error in the code - it may be a minor error they have not noticed yet. – Niall Jan 08 '16 at 07:40
  • Okay, thaks a lot of all explanations ;) – Maegpaj Jan 08 '16 at 10:05