# Header 1 includes Header 2 Which Includes Header 1, How to solve?

This topic is 4872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Thats simply it, this returns erros in VC6 Hows this solved? ace

##### Share on other sites
So Header1 includes Header2 and then Header2 includes Header1 (just getting the facts straight).

Well, both of the headers start with something like this:
// Code
#endif

Well, include similar tags around the #include statements.

//Header1:#ifndef HEADER1_H#define HEADER1_H#ifndef HEADER2_H#include"Header2.h"#endif// Rest of code#endif//Header2:#ifndef HEADER2_H#define HEADER2_H#ifndef HEADER1_H#include"Header1.h"#endif// Rest of code#endif

But the best way (well I think it is). Is to try and prevent these kind of loops.

##### Share on other sites
Put the bits that both Header1 and Header2 need, in Header3, and make Headers 1 and 2 include 3.

##### Share on other sites
The problem is that i want to pass a pointer to a function into the other class, but since the pointer is of type class1, class2 needs the class1 header so it can recieve it,

##### Share on other sites
I believe header files should be this way:

#ifndef HEADER#define HEADER// header file contents#endif

Or maybe you have defined variables in the header files, and it complains about the objects already being defined in so-so file. It is more advisable to define your variables in the source files, not header files.

##### Share on other sites
Ok, then create a forward declaration of class1, then in the source file include the header with the definition. ( I think I got declare/define the right way round, I do tend to mix them up ).

##### Share on other sites
Ok, just to clarify here, you have something like this:
class1 - does... something.class2 - has the function that needs a class1::somefunction pointer

How does class1 need class2?

##### Share on other sites
It's called a cyclic dependency. Avoid it as much as possible, not only for headers, but also classes, modules etc... The less you have the better your code is architectured. You can use call backs very often to keep your code modular.

##### Share on other sites
You could also do:

#pragma once

// header file contents

#endif

##### Share on other sites
Quote:
 Original post by MussiYou could also do:#pragma onceinstead of:#ifndef HEADER#define HEADER// header file contents#endif

Yes, you could use #pragma once instead of include guards if you want to write invalid C++ code.

##### Share on other sites
sounds like you need a forward declartion here
// ********* header2.hclass A {  ...};// ********* header1.hclass A;class B {public:   void foo(A *lpA);};

##### Share on other sites
Quote:
 Original post by MussiYou could also do:#pragma onceinstead of:#ifndef HEADER#define HEADER// header file contents#endif

pragma once does not guarantee that the file will not get included again, it simply guarantees that the preprocessor will not read the file again.