简体   繁体   English

从非模板库中派生模板类

[英]Deriving a template class from a non-template base

I'm trying to do have Derived derive from Base: 我试图做Derived派生自Base:

class Base
{
public:
    Base() {};
};

template <class T>
class Derived : public Base
{
public:
    Derived::Derived()
    {

    }
};

This gives me the following error: 这给了我以下错误:

error C3254: 'Derived': class contains explicit override '{ctor}' but does not derive from an interface that contains the function declaration 错误C3254:'派生':类包含显式覆盖'{ctor}'但不派生自包含函数声明的接口

note: see reference to class template instantiation 'Derived' being compiled 注意:请参阅正在编译的类模板实例化'Derived'的引用

error C3244: 'Derived::Derived(void)': this method was introduced by 'Unknown>' not by 'Base' 错误C3244:'Derived :: Derived(void)':此方法由'Unknown>'引入,而不是'Base'引入

I'm totally new to templates, what simple steps am I missing? 我对模板完全不熟悉,我错过了哪些简单的步骤? This seems like a pretty basic thing. 这似乎是一个非常基本的事情。

You need to omit the "Derived::" prefix. 您需要省略“Derived ::”前缀。 You would only use it to refer to a symbol that was inherited, or injected into the namespace of the class Derived. 您只能使用它来引用已继承的符号,或者将其注入到Derived类的命名空间中。 So the only prefix that makes sense is "Base::" here. 所以唯一有意义的前缀是“Base ::”。 It has nothing to do with templates. 它与模板无关。

class Base
{
public:
    Base() {};
};

template <class T>
class Derived : public Base
{
public:
    Derived() {}
};

See a working Live Demo . 查看有效的现场演示

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

相关问题 模板类源自C ++中非模板基类的事件系统 - Event System with Template class deriving from Non-Template base class in c++ 从非模板基类派生的模板类:无法访问基类变量? - Template Class derived from Non-template base class: No access to base class variables? 继承的非模板类的范围问题(模板基,非模板派生) - Scope problems with inherited non-template class (template base, non-template derived) 从模板派生类访问非模板基类虚函数 - accessing non-template base class virtual function from template derived class 模板类从非模板基类扩展并覆盖函数参数 - Template class extending from non-template base class and overriding function parameters 从基非模板类调用派生模板类方法 - Calling derived template class method from base non-template class 共享库中非模板库的模板子类导致“类”链接错误导致未定义符号typeinfo - Template subclass of non-template base from shared library causing Undefined symbols typeinfo for 'class' link error CRTP层级类的非模板基类 - Non-template base class for CRTP hierarcy class 将非模板基类向下转换为模板化派生类:是否可能? - Downcasting non-template base class to templated derived class: is it possible? 访问模板化 class 的非模板基的 static 数据 - Access static data of non-template base of templated class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM