I've read a lot of articles about resolving dependencies and about forward declaration but none of
them seem to work for me.
Basically I've got the Component abstract class which is the base class for all elements like
Buttons, Labels, CheckBoxes, Containers and so on. And every Component has a parent of type
Container. But here is the thing, the Container extends the Component because it should have the
same properties as Component and it also has a list of Components that belong to it.
I've tried the forward declaration but it didn't help.
Here is an example of what I mean, I can't really give you the full source code, coz it's just to big.
Expand|Select|Wrap|Line Numbers
- /**
- * Container.h file
- */
- #ifndef _CONTAINER_H_
- #define _CONTAINER_H_
- #include "Component.h" // If I comment this out it gives an error about the
- // invalid use of undefined type 'struct Component'
- // see the comment below
- class Component; // Forward declaration.
- class Container : public Component
- { // The compiler complains here!
- public:
- Container();
- virtual ~Container();
- Component* getChild() const;
- void setChild(const Component* c);
- // Some other methods
- private:
- Component* _child;
- // Some other attributes
- };
- #endif // _CONTAINER_H_
Expand|Select|Wrap|Line Numbers
- /**
- * Component.h file
- */
- #ifndef _COMPONENT_H_
- #define _COMPONENT_H_
- #include "Container.h"
- class Container; // Forward declaration.
- class Component
- {
- public:
- Component();
- virtual ~Component();
- Container* getParent() const;
- void setParent(const Container* c);
- // Some other methods
- private:
- Container* _parent;
- // Some other attributes
- };
- #endif // _COMPONENT_H_
Expand|Select|Wrap|Line Numbers
- /**
- * Component.cpp file
- */
- #include "Container.h"
- #include "Component.h"
- Component::Component()
- : // Some initialisations
- {
- }
- Component::~Component()
- {
- // If this Component gets destroyed I've to remove it
- // from the parent's list first.
- if (_parent != 0)
- _parent->remove(this); // This gives me an error:
- // invalid use of undefined type 'struct Container'
- }
- // Here goes the rest of the methods
I've to implement, I mean each gui element has some common properties
that I keep in the Component class. The only thing is that each Component
must belong to some Container, which has the same properties as other Components.
Any ideas how to solve such problem?
Thanks.