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
Here is the error..
error: expected class-name before '{' token
class classA : public classB {
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 theretheir 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.
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
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.