Inheritance across multiple libary headers

Hi - Im in the process of porting a big project to a libary, I was hoping to use a class from one header file as a parent to a class in a separate header files. But i have been going round in include circles trying to get it to work.

The two header files are both sub headers of the main libary header.
I have tried forward declaration - with no luck.
I have tried many different include arangments - hoping just not the right one. But fear this might not be possible without putting all my classes into one header.
Enough trying to explain i'll let the code do the talking :slight_smile:

Here is the error..

error: expected class-name before '{' token
 class classA : public classB {

In the main libary file:

#ifndef test_H
#define test_H
#include <Arduino.h>

#include "headerB.h"
#include "headerA.h"

#endif

headerA.h

#ifndef A_H
#define A_H
#include "test_lib.h"

class classA : public classB {
  public:
    void somefuntion();
};
#endif

headerB.h

#ifndef B_H
#define B_H
#include "test_lib.h"

class classB {
  public:
    void anotherfuntion();
};
#endif

thanks :slight_smile:

You're messing things up with test_lib.h and the circular references. There's no need for it to be so convoluted:

headerB.h:

#ifndef B_H
#define B_H

class classB {
  public:
    void anotherfuntion();
};
#endif

headerA.h:

#ifndef A_H
#define A_H
#include "headerB.h"

class classA : public classB {
  public:
    void somefuntion();
};
#endif

Main .ino file:

#include "headerA.h"

void setup() {
}

void loop() {
}

Thanks for that - i can see it wold work like this. But im ideally looking for a solution to have iheritance not from the primary header file. The final libary will have six different classes, with a number a of different discrepancies. This solution wold require the majority of the code to all be in the primary header - which will get pretty large. It would be preferable for them each to have there own header.
Maybe this is not possible put it would make a big difference so ill keep looking for now.
Thanks again.

howza:
It would be preferable for them each to have there their own header.

That's what the solution in Reply #1 provides. Each of the classes has its own .h file. If class A is to inherit from Class B, then it must know what class B looks like That's why headerA.h #include(s) headerB.h.

I suspect you have a fundamental misunderstanding how #include and class definitions work. But, the description of what you're trying to do is not sufficiently clear.

Thanks for the clarification, I think i can now see how this solution would scale.
So if i had a headerC and class C also depended on B. Then headerB would again be included, this time in headerC.

This complies, but would be great if you could confirm i'm heading in the right direction.

headerC.h

#ifndef C_H
#define C_H
#include <Arduino.h>
#include "headerB.h"
class classC : public classB{
  public:
    void thisfuntion();
};
#endif

and added to headerA.h

#include "headerC.h"

So every header file needs to include the headers of any dependencies, regardless if that header has been included previously.

Thanks again.

howza:
and added to headerA.h

#include "headerC.h"

If Class A does not inherit from Class C, then headerA.h does NOT need to include headerC.h.

Would i not have to include headerC - if the final library is called headerA , so i only have to include headerA in a sketch to use the whole library ?
thanks :slight_smile:

If you're bent on having only one #include file in your main code, then (if were my project) I'd put the DECLARATIONS for all the classes and their inheritance relationships all in that one common .h file rather than relying on a convoluted series of #include(s) within #include(s). You could then put the IMPLEMENTAION of the classes each in their own .cpp file. That way both your main code file and all the different .cpp implementation files would just need to #include the one common .h file.

Thank you - i like that solution.
Really appreciate you taking the time to help me work this through.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.