使用模板模板参数时出错 [英] Errors while using templates templates parameters

查看:19
本文介绍了使用模板模板参数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己正在研究 CPP 模板,但在尝试类的模板参数模板时卡住了.尝试实例化类成员时出现错误.

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  

推荐答案

您错误地声明了模板模板参数.改变

You're declaring template template parameter incorrectly. Change

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

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

顺便说一句:std::list 有两个模板参数(第二个有默认值),但是 T 只有一个;他们不匹配.由于 C++17 它工作正常,但如果您的编译器不能很好地支持 C++17,您可能必须使用参数包来解决问题.

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

这篇关于使用模板模板参数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆