How to prevent circular #include

J

junw2000

In the example below:

------------------------------
f1.h

#include "f2.h"

class A1{

void g();
}

class A2{}

--------------------------------
f1.cpp

#include "f1.h"

void A1::g(){

B1 b;
B1::h();
}

---------------------------------
f2.h

#include "f1.h"
class B1{
static void h();
}
--------------------------------
f2.cpp

#include "f2.h"
void B1::h(){
A2 a;

}

How to solve this problem other than merging f1.h and f2.h?

Thanks.

Jack
 
H

hit_pc

In the example below:

------------------------------
f1.h

#include "f2.h"

class A1{

void g();
}

class A2{}

--------------------------------
f1.cpp

#include "f1.h"

void A1::g(){

B1 b;
B1::h();
}

---------------------------------
f2.h

#include "f1.h"
class B1{
static void h();
}
--------------------------------
f2.cpp

#include "f2.h"
void B1::h(){
A2 a;

}

How to solve this problem other than merging f1.h and f2.h?

Thanks.

Jack
#ifndef F2_H
#define F2_H
blablabla
……
#endif /*F2_H*/
 
G

Grizlyk

In the example below:
This is C-language question

a) remove "#include"s from "*.h"
b) if can not remove, use "#ifndef/define" in each "*.h"

/*
LIBNAME is your libname
FILE_NAME is current "*.h" file name
*/

#ifndef LIBNAME##_##FILE_NAME
#define LIBNAME##_##FILE_NAME
#include "f2.h"

class A1{

void g();
}

class A2{}

#endif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top