Forward Declaration & Circular Dependency

Why does a forward declaration not fix the circular dependency?

The problem is that you're forward declaring TopLevel outside the foo namespace so the compiler is never finding the class foo::TopLevel.

Try moving the forward declaration of TopLevel inside the foo namespace.

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;


Related Topics



Leave a reply



Submit