数据成员“vec"不能是成员模板 [英] data member 'vec' cannot be a member template

查看:61
本文介绍了数据成员“vec"不能是成员模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了声明一个包含模板的向量,我在标题中有以下两行:

I have the following two lines in a header in order to declare a vector containing a template:

template <class t>
std::vector <t> vec;

但是我收到以下错误:

data member 'vec' cannot be a member template

我做错了什么?

我不知道我是否被正确理解,我正在尝试声明一个包含模板的向量,我知道可以这样做,因为可以具有以下内容:

I don't know that I was understood correctly, I am trying to declare a vector which contains a template, I know that this can be done since one can have the following:

template <class T>
void funct(vector <T> v){

}

该函数以模板的向量作为参数.我希望做同样的事情,除了在标题中声明向量以允许向量包含任何内容.

This function takes a vector of a template as its parameter. I wish to do the same thing except with declaring the vector in a header in order to allow the vector to contain anything.

推荐答案

template <> 语句仅用于声明函数模板类模板.例如,您可以在声明(和定义)一个类时使用它:

The template <> statement is only used when declaring a function template or a class template. For example you can use it when you declare (and define) a class:

template <typename T>
class TemplateClass {
    /* definition */
};

或者一个函数:

template <typename T>
void templateFunc(T value) {
    /* definition */
}

在创建类的实例时,不能使用模板<>语句.相反,您可以指定这样的模板参数:

When creating an instance of the class, you can't use the template <> statement. Instead you specify a template parameter like this:

TemplateClass<int> tc;

当调用模板函数时:

int i = 1;
templateFunc(i); // <-- Automatic template deduction to int.

这篇关于数据成员“vec"不能是成员模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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