简体   繁体   English

从非模板基类派生的模板类:无法访问基类变量?

[英]Template Class derived from Non-template base class: No access to base class variables?

I have the following class structure: 我具有以下类结构:

class Base {
public:
    std::set<Index> openIndices;
    Base() {};
};


template<typename lhs_t, typename rhs_t>
class Derived : public Base {
public:
    lhs_t &lhs;
    rhs_t &rhs;

    Derived(lhs_t &_lhs, rhs_t &_rhs) : 
            Base(), 
            lhs(_lhs),
            rhs(_rhs),
            openIndices(std::set_symmetric_difference(lhs.openIndices, rhs.openIndices)) 
    {
    }
};

So basicly a template Class Derived derived from the base class Base . 因此从根本上讲,是从基类Base Derived的模板类Derived When accessing the member variables ob the baser class I get the following error: 在访问基类的成员变量时,出现以下错误:

test.h:34:88: error: class ‘Derived<lhs_t, rhs_t>’ does not have any field named ‘openIndices’

I am aware of this question : I cannot access the member variables in case my class is derived from a template class. 我知道这个问题 :万一我的类是从模板类派生的,我将无法访问成员变量。 But in my case I am not derived from a template class, still I can't access the member variables. 但是就我而言,我不是从模板类派生的,但仍然无法访问成员变量。 Could anyone tell me why? 谁能告诉我为什么?

You cant initialize member variables of a base class. 您不能初始化基类的成员变量。 You have to provide an appropriate constructor in the base class and call this constructor. 您必须在基类中提供适当的构造函数,然后调用该构造函数。

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

相关问题 将非模板基类向下转换为模板化派生类:是否可能? - Downcasting non-template base class to templated derived class: is it possible? 继承的非模板类的范围问题(模板基,非模板派生) - Scope problems with inherited non-template class (template base, non-template derived) 从模板派生类访问非模板基类虚函数 - accessing non-template base class virtual function from template derived class 从基非模板类调用派生模板类方法 - Calling derived template class method from base non-template class 在从模板化中介派生的 class 中调用非模板基 class 构造函数 - Calling non-template base class constructor in class derived from templated intermediary 从非模板基 class 指针调用模板派生 class 方法的方法 - Invoke a method of templated derived class method from non-template base class pointer 从非模板库中派生模板类 - Deriving a template class from a non-template base 如何使用非模板派生的 class 制作非类型模板基础 class - How to make a non-type template base class with non-template derived class 访问模板化 class 的非模板基的 static 数据 - Access static data of non-template base of templated class 如何将模板派生类的方法提取到非模板基类中 - how to extract template derived class's method into non-template base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM