Я сам изучаю шаблоны CPP и застрял при попытке попробовать шаблоны шаблонов параметров для класса.Я получаю ошибки, когда пытаюсь создать экземпляр класса.
#pragma once
#include "stdafx.h"
# include <list>
template<class type, template<type> class T>
class stack
{
private:
int count;
int size;
T<type> st;
public:
stack(size_t size):size(100), count(-1){ }
void push(type elem);
type pop();
};
template<class type, template<type> class T>
void stack<type, T>::push(type elem)
{
if (count < (size - 1))
{
st.push_back(elem);
count++;
}
else
{
cout << "Elements cannot be added to the internal stack" << endl;
}
}
template<class type, template<type> class T>
type stack<type, T>::pop()
{
if (count < 0)
{
cout << "Stack is empty," << endl;
}
else
{
T elem = st.back();
st.pop_back();
count--;
return elem;
}
}
void test_stack_class()
{
stack<int, list> s1(10);
/*s1.push(vec);
s1.push(22);
s1.push(40);
s1.push(12);
s1.push(7);
cout << "Stacks pops: " << s1.pop() << endl;
cout << "Stacks pops: " << s1.pop() << endl;
*/
}
Пожалуйста, помогите мне преодолеть эту ошибку и постарайтесь объяснить немного.Я думаю, что у меня слабое понимание шаблонов, что даже после обращения ко многим интернет-сайтам я не могу решить эту проблему.
Обновление: - Я получаю эти ошибки.
Severity Code Description Project File Line Suppression State
Error C2514 'stack': class has no constructors templates_learning c:\users\bhpant\source\repos\templates_learning\templates_learning\stack.h 58
Error C2993 'type': illegal type for non-type template parameter '__formal' templates_learning c:\users\bhpant\source\repos\templates_learning\templates_learning\stack.h 12
Error (active) E0999 class template "std::list" is not compatible with template template parameter "T" templates_learning c:\Users\bhpant\source\repos\templates_learning\templates_learning\stack.h 58
Error C3201 the template parameter list for class template 'std::list' does not match the template parameter list for template parameter 'T' templates_learning c:\users\bhpant\source\repos\templates_learning\templates_learning\stack.h 58