Using structs of derived class y base template - c++

I have a base template:
template <typename T>
class Base
{
public:
void method(T);
};
And a derived class:
class Derived: public Base<Derived::status_t>
{
public:
typedef struct
{
uint8_t value;
} status_t;
}
I'm have more derived classes, each one with it status_t specific struct. I want to use these structs in the base class, but the compiler give me an error:
Error[Pe070]: incomplete type is not allowed.
I suppose that the problem is that the struct is not defined in the moment that the base constructor is called. Is there any way to maintain the struct in the derived class and use it un the base template?
Thanks,

Related

How to call the base class method in C++?

I am working on a C++ program. For one of the use case, I have a class which is derived from its template class. So, I'm wondering how we can call the base class method inside the derived class method?
Example:
template <typename base>
struct derived : public base
{
void aFunction()
{
// need to call a base function() here
}
};
One of the way could be something like base::aBaseFunction(), but I am not sure?
I am new to OOP programming, so looking forward to learning a new concept through this problem statement.
If you want to explicitly use the base's member, make the type explicit like you found:
template <typename base>
struct derived : public base
{
void aFunction()
{
base::function();
}
};
If you would rather have the usual unqualified-lookup behaviour, make this explicit instead:
template <typename base>
struct derived : public base
{
void aFunction()
{
this->function();
}
};
Both of these behave exactly like they do in non-templates.

Inheritance with template classes

I'm trying to figure out, how can I inherit from template class to template class. The problem is: I can't use protected members of Parent class.
Example:
template <class N>
class Parent {
protected:
N member;
public:
Parent(N aa){
member = aa;
}
};
class Child1: public Parent<int>{
public:
Child1(int a): Parent<int>(a) {
member += 1; // works
}
};
template<class Q>
class Child2: public Parent<Q>{
public:
Child2(int a): Parent<Q>(a) {
member += 1; // does not work (use of undeclared identifier)
}
};
How can I use "member" in the Child2 class?
Thanks for your time
You need to use this->member or Parent<Q>::member.
In the second case, member is a "dependent name" because the existence of member from the base class template Parent<Q> depends on the type of class Q in the template, whereas in the first example there is no dependent type, the compiler can statically analyze that Parent<int> contains member.

Inhertiance with Templates - Variable does not name a type

I have the following code example below, with two template classes, one base and one derived. I need to access a type in the base class from the derived class, but it says that it does not name a type. Why is this the case?
'Parameter' does not name a type
using namespace std;
template<typename PointT>
class BaseClass{
public:
BaseClass(){}
class Parameter{
Parameter(){}
};
};
template<typename PointT>
class DerivedClass : public BaseClass<PointT>{
public:
DerivedClass(){}
class ParameterExtended{
Parameter x;
};
};
You need this:
typename BaseClass<PointT>::Parameter x;

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?
You cant initialize member variables of a base class. You have to provide an appropriate constructor in the base class and call this constructor.

How do I forward declare a class derived from a forward declared template base class?

I am trying to forward declare a class that is derived from a template class that must also be forward declared.
Here is an example of the classes:
class TType {
public:
TType() { }
};
template<typename T>
class Base {
public:
Base() { }
};
class Derived : public Base<TType> {
public:
Derived() { }
};
Here is a failed guess at what I need:
class TType;
template<typename T> class Base;
class Derived : public Base<TType>; // This fails
Derived* pDerived;
Just forward declare the class name:
class Derived;
You can't include any more information about a class in its declaration; base classes, members, etc. can only be declared in the class definition.
This forward declaration can be used to do various things, including declaring pointers or references (such as pDerived in your example), and also declaring functions with Derived as an argument or return type. If you need to do anything that needs to know the class's size, base classes, or members, then you'll need the full definition.