[英]Scope problems with inherited non-template class (template base, non-template derived)
I am trying to create a templated base class with non-templated derived classes. 我试图用非模板派生类创建一个模板化基类。 I have been following umsl.edu/~subramaniana/templates8.html and http://www.cplusplus.com/doc/tutorial/templates/ to do so.
我一直在关注umsl.edu/~subramaniana/templates8.html和http://www.cplusplus.com/doc/tutorial/templates/来做到这一点。
template <class Type>
class Base {
protected:
std::string line;
public:
Base();
};
class DerivedA : public Base<T> {
//error: 'T' was not declared in this scope
//error: template argument 1 is invalid
public:
DerivedA();
protected:
std::list<std::string> A;
};
I think I am missing something fundamental about how this all works, but I can't seem to grasp it. 我想我缺少关于这一切工作原理的基本知识,但我似乎无法掌握。
This is the full header and implementation: 这是完整的标头和实现:
You missed template<typename T>
in class DerivedA declaration. 您错过了类DerivedA声明中的
template<typename T>
。 Base is a template, you need to provide template parameter to it. Base是模板,您需要为其提供模板参数。
template<typename T>
class DerivedA : public Base<T>
Or you could let DerivedA derive from a certain type of Base, for example: 或者,您可以让DerivedA从某种类型的Base派生,例如:
class DerivedA : public Base<int>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.