C ++ 11中的模板模板错误

Slifer龙

我一直在阅读“ C ++模板,完整指南”的第5章,并且已经看到了“模板模板”模板的概念,因此请尝试一下。

在模板类中,我已经这样声明了我的模板:

template <typename TipoClave, typename TipoDato,
         template <class Elem, class Alloc = allocator<Elem>> class Lista = vector>

这可行。当我尝试使用默认容器以外的容器时,出现了我的问题。

我的课如下:

class Tabla
{
public:
    struct Celda {
        TipoClave clave;
        TipoDato dato;
    };

    Tabla(unsigned tam)
    {
        t.resize(tam);
    }

///< Some other functions

private:
    typedef Lista<Celda> ListaDatos; 
    Lista<ListaDatos> t;
};

然后,当我尝试从这样的主程序中使用它时:

int main (void)
{
    Tabla<string,Alumno,array> tabla(200);
    ///< Some stuff

    return 0;
}

但是此行Tabla<string,Alumno,array> tabla(200);无法编译,使我得到如下错误:

test_tabla_t.cpp:在函数'int main()'中:test_tabla_t.cpp:20:27:错误:'模板类Lista>类Tabla'的模板参数列表中参数3的类型/值不匹配Tabla tabla(200);

我尝试使用Tabla<string,Alumno,vector> tabla(200);,并且可以使用,所以我不知道如何解决此错误。

jared_schmitz

假设您尝试使用std::array,它不采用分配器模板参数。它的第二个参数是数组的大小。

template< 
    class T, 
    std::size_t N 
> struct array;

您没有提到您对C ++的经验,但是由于您想学习更多,所以我会说我已经使用C ++近十年了,一方面可以指望我曾经经历过的次数。使用的模板模板参数。

在这种情况下,您想Lista成为“像序列容器[1]一样的东西”,您可能想阅读有关concept [2]的知识,这可能会使它成为C ++ 20。它们使您可以告诉编译器您希望模板参数具有的接口。

同时,将Lista声明为纯模板参数可能会更容易。

[1] http://en.cppreference.com/w/cpp/concept/SequenceContainer
[2] https://en.wikipedia.org/wiki/Concepts_(C%2B%2B)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章