Help me understand (headers)

Ok so I just started working with making classes into files. Working on a friend operator I came into a problem, headers in .h files. For some reason when I add headers for other classes in my .h files it either breaks or fixes my errors. I would like an explanation for the right and wrong ways to use headers instead of trying them randomly until the program works.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

CLASS 1 .H FILE
 #ifndef CLASS1_H
#define CLASS1_H
#include <stdio.h>
// if i put #include "Class2.h" here this breaks the code WHY!?
class Class1
{
    public:
        Class1();

    protected:

    private:

        int var=10;
        friend class Class2;
};

#endif
CLASS 2 .H FILE
#ifndef CLASS2_H
#define CLASS2_H
#include "Class1.h" // Without this here, program breaks(error class 1 not defined in scope) why?
#include <stdio.h>



class Class2
{

    public:

void getVar(Class1 obj);

};

#endif  

thanks for your time!
Last edited on
You create a circular inclusion. This article has a good deal of information that eventually addresses your problem:
http://www.cplusplus.com/articles/Gw6AC542/
Thank you so much Zhuge, this is an amazing amount of help!
Topic archived. No new replies allowed.