简体   繁体   English

类成员:模板化类的向量

[英]Class member: Vector of templatized class

I have a class that has a member that is a vector of another class that is templatized. 我有一个具有成员的类,该成员是另一个已模板化的类的向量。

My code in classA.h looks like this: 我在classA.h中的代码如下所示:

template <typename T> vector<classB<T> >

I get errors such as "no appropriate default constructor is available" and "multiple template parameter lists are not allowed." 我收到诸如“没有合适的默认构造函数可用”和“不允许多个模板参数列表”之类的错误。 I previously had no errors in classB until I added this line to classA. 在将这行添加到classA之前,我以前在classB中没有错误。

I don't have a lot of experience using templates and don't know why it isn't compiling, or what exactly the syntax should be. 我没有很多使用模板的经验,也不知道它为什么不编译,或者语法应该是什么。 this question seems to help but the answer is too terse for me to understand fully. 这个问题似乎有帮助,但答案太紧,我无法完全理解。 Is it really necessary to make a base class and derive from it as that answer suggests? 如答案所示,是否真的有必要建立基类并从中派生? Does class A need to be templatized as well since it has this vector member? 由于类A具有此向量成员,是否也需要对它们进行模板化?

What's going on? 这是怎么回事?

any help is much appreciated 任何帮助深表感谢

You are actually declaring a template in the specified snipped. 您实际上是在指定的片段中声明一个模板。 If that's a member of a class the class must be a template and propagate the type information in the member, something like 如果那是一个类的成员,则该类必须是模板,并在该成员中传播类型信息,例如

template <typename T>
class Test {
     vector<MyClass<T>> _vector;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM