简体   繁体   中英

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:

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.

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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