Circular dependency between C header files
The solution is to simply use some manner of program design. Each "object"/"module" in your program should consist of one h file and one c file. The h file is the public interface. Each such object should only be concerned with its own designated task. It should only include the resources needed to perform that task.
With such a design, there should never be any circular dependencies, or the design is flawed. You should not fix a bad design with various code tricks, you should re-do the design.
But of course the same resource could be included multiple time from different parts of the code. This is why we always use header guards in every single h file.
Circular C++ Header Includes
Is this kind of cross-inclusions are prohibited?
Yes.
A work-around would be to say that the ifr member of mainw is a reference or a pointer, so that a forward-declaration will do instead of including the full declaration, like:
//#include "IFr.h" //not this
class IFr; //this instead
...
class mainw
{
public:
static IFr* ifr; //pointer; don't forget to initialize this in mainw.cpp!
static CSize=100;
...
}
Alternatively, define the CSize value in a separate header file (so that Ifr.h can include this other header file instead of including mainw.h).
C++ circular header includes
Is there a way to forward declare these classes in a way that will allow me to not care about the order in which I include the header files in my main.cpp?
since you are dealing with simple pointers only, you can use a forward declaration here in both cases:
FooA.h
#ifndef H_FOOA
#define H_FOOA
// #include "foob.h" << not needed!
class FooB; // << substitute with a forward declaration of FooB
class FooA{
public:
FooB *fooB;
};
#endif
FooB.h
#ifndef H_FOOB
#define H_FOOB
class FooA;
class FooB{
public:
FooA *fooA;
};
#endif
Fixing circular dependencies c++17 headers
The usual fix is simple:
struct Xchart; // declares Xchart; definition is elsewhere.
short WINAPI MyFunction(Xchart *mychart); // Function declaration.
Only toolkit.cpp will need the definition of Xchart
, but .cpp files themselves are not included elsewhere and don't contribute to circular inclusions.
Circular dependency in single C header file. Forward declaration needed?
You can make the declaration of a struct
before its definition:
/* declaration */
struct foo;
.....
/* definition */
struct foo
{
....
};
Anywhere you write struct foo
is a declaration of the struct, so you don't have to put it in a separate line, you can have it in a typedef, pointer declaration, etc..
Just be aware that sometimes, like in variable declarations of type struct foo
you also need the definition (to calculate the variable size);
/* declare struct foo ..*/
struct foo;
/* .. or declare struct foo ..*/
typedef struct foo foo;
/* .. or declare struct foo ..*/
struct foo *p;
/* .. or declare struct foo */
int bar (struct foo *p);
/* Not valid since we don't have definition yet */
struct foo f1;
/* definition */
struct foo
{
....
};
/* Valid */
struct foo f2;
In your case you haven't given the struct a name; you've just made a typedef
that is an alias for an anonymous struct. So to forward declare your struct you have to give it a name:
/*
forward declare the `struct stage_table_context_t` and give it a typedef alias
with the same name as the structs name
*/
typedef struct stage_table_context_t stage_table_context_t;
typedef stage_table_context_t* (*stage_table_function_t)(stage_table_context_t*);
typedef struct {
const char* stage_name;
stage_table_function_t* function;
} stage_t;
struct stage_table_context_t{
uint32_t error_number;
stage_t* current_stage;
} stage_table_context_t;
Solving a circular dependency - C
It can be useful to define enums in their own file(s), and if you do that here, your problem will disappear.
Related Topics
Avoiding the Tedium of Optional Parameters
Determine Process Info Programmatically in Darwin/Osx
C++: Can Vector<Base> Contain Objects of Type Derived
How to Validate Numeric Input in C++
Why Is Iterating Though 'Std::Vector' Faster Than Iterating Though 'Std::Array'
The Precision of Std::To_String(Double)
Using 'Std::Function<Void(...)>' to Call Non-Void Function
Partial Class Template Argument Deduction in C++17
How Does Virtual Method Invocation Work in C++
Concat Two 'Const Char' String Literals
Blur Effect Over a Qwidget in Qt
Division by Zero: Undefined Behavior or Implementation Defined in C And/Or C++
Is There a Use for Function Declarations Inside Functions
How Define an Array of Function Pointers in C
How to Determine If a Type Is a Scoped Enumeration Type