Misaka_Mikoto
Misaka_Mikoto

Reputation: 33

#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.

Upvotes: 3

Views: 243

Answers (4)

Cole Tobin
Cole Tobin

Reputation: 9426

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.

Upvotes: 1

ashish.elex
ashish.elex

Reputation: 1

You are creating cyclic dependency. in resource.h you have included a.h , b.h and c.h which is not required. class files needs resource, resource file does not need class information.

Upvotes: 0

A.E. Drew
A.E. Drew

Reputation: 2137

c.h includes resource.h which then itself includes a.h and b.h which each include resource.h again.

Upvotes: 0

Cengiz Kandemir
Cengiz Kandemir

Reputation: 375

You can use #pragma once preprocessor to make resource.h to be included only once in compilation.

Upvotes: 0

Related Questions