Errors while using templates templates parameters

让人想犯罪 __ 提交于 2021-01-05 06:39:17

问题


I am studying CPP templates myself and got stuck while trying templates of templates parameters for a class. I am getting errors when I am trying to instantiate the class member.

#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;
*/

}

Kindly help me get over this error and try to explain a bit. I think somehwere my understanding about templates is weak that even after referring many online sites I am not able to get to the issue.

Update:- I am getting these errors.

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  

回答1:


You're declaring template template parameter incorrectly. Change

template<class type, template<type> class T>
class stack

to

template<class type, template<typename> class T>
class stack

BTW: std::list has two template parameters (the 2nd one has default value), but T has only one; they don't match. Since C++17 it works fine but if your compiler doesn't support C++17 well you might have to use parameter pack to solve the issue.

template<class type, template<typename...> class T>
class stack


来源:https://stackoverflow.com/questions/50656291/errors-while-using-templates-templates-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!