0

I realize this question has been asked many times and the solution is usually forward declarations. However, if I am correct, forward declarations can't be used when member functions are being used in that file. I have a circular dependency that I'm not sure how to fix through design and each dependency uses member functions. Here is my code:

// GUI.h
#include "ElementButton.h"

class GUI
{
public:
    vector<ElementButton*> Buttons;
    void ParseElements(); // Uses ElementButton member functions
};


// Element.h
#include "GUI.h"

class Element
{
public:
    void ReplaceGUI(string GUITextFile); // Uses GUI member functions and destructors
    GUI* CurrentGUI;
};


// ElementButton.h
#include "Element.h"

class ElementButton : public Element
{
public:
    void OnButtonClicked(); // Uses inherited ReplaceGUI function
};
epitaque
  • 73
  • 3
  • 9
  • 2
    What do you mean by "that file"? Put the implementations in the .cpp files, and everything will work with forward declarations. – molbdnilo Jun 03 '15 at 23:21
  • Sorry for responding early. Pressing enter submits a comment. I'll edit this comment soon. – epitaque Jun 03 '15 at 23:29
  • A forward declaration doesn't allow you to use member functions, but it doesn't prevent you either. In particular, you could include the full definition later in the translation unit. – user253751 Jun 03 '15 at 23:34
  • I don't see the problem. Based on the sample given, this should be resolved easily in the cpp file by including the appropriate headers. – user4581301 Jun 03 '15 at 23:37
  • That was my problem. I mistakenly thought including header files in .cpp files would somehow still create a circular dependency, not sure why. I can see how the #includes actually don't go in a circle. Here was the edit I was going to make: "Say you have `Class A`. Class A.hpp forward declares `Class B`, and `Class B` has member functions. Class A.cpp can't use member functions of `Class B` because 'Class A' is not including 'Class B' in any way; it is only forward declaring it. Is there some way of including `Class B` only in the .cpp file?" but I think I get it now. Thanks for the help. – epitaque Jun 03 '15 at 23:41

1 Answers1

1

You don't have to defined a class in order to return a pointer. Add one more header ForwardDeclarations.h like this:

class GUI;
class Element;
class ElementButton;

and include it in all your header files:

// GUI.h
#include "ForwardDeclarations.h"

class GUI
{
public:
    vector<ElementButton*> Buttons;
    void ParseElements(); // Uses ElementButton member functions
};


// Element.h
#include "ForwardDeclarations.h"

class Element
{
public:
    void ReplaceGUI(string GUITextFile); // Uses GUI member functions and destructors
    GUI* CurrentGUI;
};


// ElementButton.h
#include "ForwardDeclarations.h"
#include "Element.h"

class ElementButton : public Element
{
public:
    void OnButtonClicked(); // Uses inherited ReplaceGUI function
};

Notice that ElementButton.h still needs to include Element.h because deriving class ElementButton from class Element requires class definition.

Michael
  • 5,775
  • 2
  • 34
  • 53