Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Derivedderived from the base class Base. 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?

like image 417
Haatschii Avatar asked Feb 13 '23 19:02

Haatschii


1 Answers

You cant initialize member variables of a base class. You have to provide an appropriate constructor in the base class and call this constructor.

like image 193
Sebastian Hoffmann Avatar answered May 08 '23 00:05

Sebastian Hoffmann