![](/img/trans.png)
[英]Downcasting non-template base class to templated derived class: is it possible?
[英]Scope problems with inherited non-template class (template base, non-template derived)
我试图用非模板派生类创建一个模板化基类。 我一直在关注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;
};
我想我缺少关于这一切工作原理的基本知识,但我似乎无法掌握。
这是完整的标头和实现:
您错过了类DerivedA声明中的template<typename T>
。 Base是模板,您需要为其提供模板参数。
template<typename T>
class DerivedA : public Base<T>
或者,您可以让DerivedA从某种类型的Base派生,例如:
class DerivedA : public Base<int>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.