Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include cyclic dependency error

I have code structure like this:

resource.h:

#include"a.h"
#include"b.h"
#include"c.h"

a.h:

#ifndef __A__
#define __A__
#include"resource.h"
class B;
class A{
//something uses B
};
#endif

b.h:

#ifndef __B__
#define __B__
#include"resource.h"
class A;
class B{
//something uses A
}
#endif

c.h:

#ifndef __C__
#define __C__
#include"resource.h"
class A;
class B;
class C{
//something uses A and B
};
#endif

The problem is the following: VS2010 tells me that in c.h, line #include"resource.h" causes "resource.h" includes itself.

However, the codes are able to compile and performed as expected. So I am wondering what causes this error intellisense in VS and if there is anyway to remove it.

P.S: I am compiling with VS and there is no compiling error.

like image 587
Misaka_Mikoto Avatar asked Jun 06 '26 07:06

Misaka_Mikoto


1 Answers

You don't have a header guard on resource.h:

#ifndef __RESOURCE__
#define __RESOURCE__ 1
#include "a.h"
#include "b.h"
#include "c.h"
#endif

However, double underscores aren't recommended, as they're reserved for the implementation. So I would use {PROJECTNAME}_RESOURCE_H. This will also prevent header guard collisions with other projects that don't do this.

Seeing that you're using Visial Studio, I would reccomend you don't use header guards and instead use #pragma once if your project isn't going I be compiled with gcc.

like image 105
Cole Tobin Avatar answered Jun 07 '26 23:06

Cole Tobin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!