Template class specialization

I read some related topics, but the problem is still not clear:

#include <stdio.h> #include <vector> #include <iostream> template <> class stack <int> { public: std :: vector <int> stackVector; }; 

Compilation Error:

 templateSpecializ.cpp:5: error: 'stack' is not a template templateSpecializ.cpp:6: error: explicit specialization of non-template 'stack' 

With this link: coderSource.net

Am I missing a point? I feel like I have. I even tried to define the functions there, but that did not help.

+4
source share
2 answers

This is a template specialization of a template called a stack. the stack is not defined in any of these header files. If you want to define a new class of patterns, you must first define the base case

 template<typename T> class stack { //implementation goes here }; template<> class stack<int> { public: std::vector<int> stackVector; }; 

If you want to define a stack only for int, and not for each type, you can use

 template<typename T> class stack; template<> class stack<int> { public: std::vector<int> stackVector; }; 
+7
source

You cannot specialize your template unless you have a template for specialization. Therefore, this should work:

 template <typename T> class stack { }; template <> class stack<int> { public: std::vector<int> stackVector; }; 
+2
source

Source: https://habr.com/ru/post/1346193/


All Articles