1

so I just can't work out my circular inclusions. Can anyone give me a hand? I know I need to be pre-declaring my classes, but I can not work out the combination (mostly guess-work though). OtherClass is meant to be like a container for ParentClass and its children.

My current class setup is along the lines of this:

OtherClass.h

class ParentClass; // includes
class ChildClass;

class OtherClass
{
    ParentClass* parent;
    ChildClass* child;
}

ParentClass.h

class OtherClass;
class ParentClass
{
    OtherClass* other;
}

ChildClass.h

#include "ParentClass.h";
#include "OtherClass.h";

class ChildClass: public ParentClass
{
    other->foo(); // Using OtherClass pointer declared in parent.
}

For that approach above, I am getting Member access into incomplete type 'ParentClass'.

This probably also needs to be expandable, as I'm sure in the future there will be more child classes of ChildClass.

HalfBit
  • 47
  • 10
  • 3
    What have you tried? there are no `#include<>` statements in this snippets and no guards... – Amit Dec 16 '15 at 10:14
  • Possible duplicate of [Resolve circular dependencies in c++](http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c) – dkg Dec 16 '15 at 10:15
  • @Amit, I edited the original question with my last attempt at it. There are also include guards around all the .h's, I was just keeping it cleaner (hopefully). – HalfBit Dec 16 '15 at 10:20
  • @dkg, not quite, as that only deals with the Parent/Child side. I need them also linking into a 3rd class. – HalfBit Dec 16 '15 at 10:20
  • you can't include the same file in your file. i.e. ChildClass.h #include ChildClass.h is not possible and makes little sense. – Raphael Müller Dec 16 '15 at 10:24
  • @RaphaelMüller. Ah yep, of course. That's just a typo, meant to be `OtherClass.h`, will edit. – HalfBit Dec 16 '15 at 10:25
  • The example has two include statements which form a tree. There is no cycle. Could you change it to show the problem? Do you want an assessment of your approach? – Jens Dec 16 '15 at 10:50
  • Can you add a little bit detail of `ChildClass`? I tried your code. And your code should be able to compile. See http://ideone.com/ilR8b7 – Danh Dec 16 '15 at 10:52
  • I updated my question, and included the error I get for that approach as well as a bit more on ChildClass. – HalfBit Dec 16 '15 at 11:07

1 Answers1

0

It's not clear what you mean by ChildClass.cpp uses things from OtherClass as well, that is, if you want to inherit from the OtherClass type or just compose of it ('has a' vs. 'is a'), but you could use templates as well to make it a generic container class for various types:

OtherClass.h

#if !defined(OTHER_CLASS_HPP)
#define OTHER_CLASS_HPP

template < typename T1, typename T2 >
class OtherClass
{
    public: // private/protected ??
        T1* parent;
        T2* child;

        void foo()
        {
            this->parent->bar();
            this->child->baz();
            // this->parent->baz(); // compile error since no ParentClass::baz
        }
};

#endif

ParentClass.h

#if !defined(PARENT_CLASS_HPP)
#define PARENT_CLASS_HPP

#include "OtherClass.h"

template < typename T1 >
class ParentClass
{
    public:
        ParentClass() : other(new OtherClass<ParentClass, T1>) {}
        ~ParentClass() { delete this->other; }

        void bar() { }
    protected:
        OtherClass<ParentClass, T1>* other;
};

#endif

ChildClass.h

#if !defined(CHILD_CLASS_HPP)
#define CHILD_CLASS_HPP

#include "ParentClass.h"

class ChildClass : public ParentClass < ChildClass >
{
    public:
        void foo()
        {
            this->other->foo();
        }

        void baz() { }
};

#endif

And depending on what each class exactly does, this can abstract a little more for you to make it 'expandable'. You don't have to forward declare anything with this approach but it can add complexity elsewhere (again, depending on the exact needs of the parent/child relationship/classes).

Hope that can help.

txtechhelp
  • 6,625
  • 1
  • 30
  • 39
  • Hm.. I'm not sure template's will solve my problem this time. The `OtherClass` is meant to be a container for `ParentClass` and it's children. When I say `ChildClass.cpp` uses things from `OtherClass`, it's just manipulating a OtherClass object set in `ParentClass`. – HalfBit Dec 16 '15 at 10:55
  • @HalfBit, I've updated my answer to match your updated question, but if `OtherClass` is supposed to be just a container for `ParentClass` and it's children, why do you have a pointer to `OtherClass` in the `ParentClass`? – txtechhelp Dec 16 '15 at 11:24
  • The pointer to its container is in case it needs to change container (so it actually points to a few different containers). That change is determined from within ParentClass, not OtherClass. – HalfBit Dec 16 '15 at 11:29