1

Drafting a project, I found it easiest to create mutually referenced structs in the main.cpp file as follows:

struct component;
struct vertex{
     component * parent;
     ...;
}
struct component{
     vector<vertex *> vertices;
     ...;
}

Before I start coding up the final version of these structs as classes in a header file, is this the accepted way to do this?

Or is there a way one is "supposed" to create mutually referencing structs/classes?

Chris
  • 28,822
  • 27
  • 83
  • 158
  • 2
    Put them in a header rather than a C++ file. Make your header file self-contained. E.g., since you are using `vector`, you will have a `#include ` in your header. However, do *NOT* have a `using namespace std;` -- use fully qualified (e.g., `std::vector< ... >` instead. – Happy Green Kid Naps Feb 19 '18 at 21:58
  • @HappyGreenKidNaps haha, yes have encountered ***that*** particular problem...thanks for the heads up. – Chris Feb 19 '18 at 21:59

1 Answers1

3

This is perfectly acceptable, and it is called "Forward Declaration." You can read more about it here and here.