0

Suppose I have a template in C++

template<typename T>
class my_class
{
    virtual void my_func(int i);
};

Now I want to partially specialize my_func to be a pure virtual function;

template<>
my_class<ExcistingClass>::my_func(int i) = 0;

but compiler gives an error

error C2988: unrecognizable template declaration/definition
error C2059: syntax error : '='

why does the compiler not recognize the logic I wrote, and how can I implement such logic correctly?

Vahag Chakhoyan
  • 873
  • 1
  • 10
  • 21
  • you cant cherrypick. YOu need to specialize the whole class – 463035818_is_not_an_ai Nov 01 '22 at 09:45
  • @463035818_is_not_a_number which part of class is not specialized? – Vahag Chakhoyan Nov 01 '22 at 09:47
  • everything is not specialized. I think you assume that because in the primary template there is only one method also in the specializations there is only one method. But thats not how it works. YOU need to specialize the whole class and in the specialization there can be anything, 0 up to as many as you like members. – 463035818_is_not_an_ai Nov 01 '22 at 09:56
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of related SO posts for this. – Jason Nov 01 '22 at 10:00
  • [Dupe1](https://stackoverflow.com/questions/15374841/c-template-partial-specialization-member-function), [Dupe2](https://stackoverflow.com/questions/165101/invalid-use-of-incomplete-type-error-with-partial-template-specialization), [Dupe3](https://stackoverflow.com/questions/32397948/why-cant-you-partially-specialize-a-class-member-function) and [Dupe4](https://stackoverflow.com/questions/68505599/workaround-to-partially-specialize-a-single-member-of-a-class-template) – Jason Nov 01 '22 at 10:06

0 Answers0