C++ #include guards

后端 未结 8 702
鱼传尺愫
鱼传尺愫 2020-11-22 13:24

SOLVED

What really helped me was that I could #include headers in the .cpp file with out causing the redefined error.


I\'m new to C++ but I have some p

相关标签:
8条回答
  • 2020-11-22 13:32

    Add include guards in all your *.h or *.hh header files (unless you have specific reasons to not do that).

    To understand what is happening, try to get the preprocessed form of your source code. With GCC, it is something like g++ -Wall -C -E yourcode.cc > yourcode.i (I have no idea on how Microsoft compilers do that). You can also ask which files are included, with GCC as g++ -Wall -H -c yourcode.cc

    0 讨论(0)
  • 2020-11-22 13:40

    The issue is that your GameObject.h does not have guards, so when you #include "GameObject.h" in Physics.h it gets included when GameObject.h includes Physics.h.

    0 讨论(0)
  • 2020-11-22 13:40

    The goal of a header guard is to avoid including the same file many times. But the header guard that is currently used in C ++ can be improved. The current guard is:

    #ifndef AAA_H
    #define AAA_H
    
    class AAA
    { /* ... */ };
    
    #endif
    

    My new guard proposal is:

    #ifndef AAA_H
    #define AAA_H
    
    class AAA
    { /* ... */ };
    
    #else
    class AAA;  // Forward declaration
    #endif
    

    This resolves the annoying problem that occurs when the AAA class needs the BBB class declaration, while the BBB class needs the AAA class declaration, typically because there are crossed pointers from one class to the other:

    // File AAA.h
    #ifndef AAA_H
    #define AAA_H
    
    #include "BBB.h"
    
    class AAA
    { 
      BBB *bbb;
    /* ... */ 
    };
    
    #else
    class AAA;  // Forward declaration
    #endif
    
    //+++++++++++++++++++++++++++++++++++++++
    
    // File BBB.h
    #ifndef BBB_H
    #define BBB_H
    
    #include "AAA.h"
    
    class BBB
    { 
      AAA *aaa;
    /* ... */ 
    };
    
    #else
    class BBB;  // Forward declaration
    #endif
    

    I would love for this to be included in the IDEs that automatically generate code from templates.

    0 讨论(0)
  • 2020-11-22 13:42

    " #pragma once " ::: serves the same purpose as header guards, and has the added benefit of being shorter and less error-prone.

    Many compilers support a simpler, alternate form of header guards using the #pragma directive: " #pragma once " // your code here

    However, #pragma once is not an official part of the C++ language, and not all compilers support it (although most modern compilers do).

    For compatibility purposes, people recommend sticking to traditional header guards. They aren’t much more work and they’re guaranteed to be supported on all compliant compilers.

    0 讨论(0)
  • 2020-11-22 13:45

    Firstly you need include guards on gameobject too, but that's not the real problem here

    If something else includes physics.h first, physics.h includes gameobject.h, you get something like this:

    class GameObject {
    ...
    };
    
    #include physics.h
    
    class Physics {
    ...
    };
    

    and the #include physics.h gets discarded because of the include guards, and you end up with a declaration of GameObject before the declaration of Physics.

    But that's a problem if you want GameObject to have a pointer to a Physics, because for htat physics would have to be declared first.

    To resolve the cycle, you can forward-declare a class instead, but only if you are just using it as a pointer or a reference in the declaration following, i.e.:

    #ifndef PHYSICS_H
    #define PHYSICS_H
    
    //  no need for this now #include "GameObject.h"
    
    #include <list>
    
    class GameObject;
    
    class Physics
    {
    private:
        list<GameObject*> objects;
        list<GameObject*>::iterator i;
    public:
        void ApplyPhysics(GameObject*);
        Vector2X CheckCollisions(Vector2X, GameObject*);
    };
    
    #endif // PHYSICS_H
    
    0 讨论(0)
  • 2020-11-22 13:48

    The preprocessor is a program that takes your program, makes some changes (for example include files (#include), macro expansion (#define), and basically everything that starts with #) and gives the "clean" result to the compiler.

    The preprocessor works like this when it sees #include:

    When you write:

    #include "some_file"
    

    The contents of some_file almost literally get copy pasted into the file including it. Now if you have:

    a.h:
    class A { int a; };
    

    And:

    b.h:
    #include "a.h"
    class B { int b; };
    

    And:

    main.cpp:
    #include "a.h"
    #include "b.h"
    

    You get:

    main.cpp:
    class A { int a; };  // From #include "a.h"
    class A { int a; };  // From #include "b.h"
    class B { int b; };  // From #include "b.h"
    

    Now you can see how A is redefined.

    When you write guards, they become like this:

    a.h:
    #ifndef A_H
    #define A_H
    class A { int a; };
    #endif
    
    b.h:
    #ifndef B_H
    #define B_H
    #include "a.h"
    class B { int b; };
    #endif
    

    So now let's look at how #includes in main would be expanded (this is exactly, like the previous case: copy-paste)

    main.cpp:
    // From #include "a.h"
    #ifndef A_H
    #define A_H
    class A { int a; };
    #endif
    // From #include "b.h"
    #ifndef B_H
    #define B_H
    #ifndef A_H          // From
    #define A_H          // #include "a.h"
    class A { int a; };  // inside
    #endif               // "b.h"
    class B { int b; };
    #endif
    

    Now let's follow the preprocessor and see what "real" code comes out of this. I will go line by line:

    // From #include "a.h"
    

    Comment. Ignore! Continue:

    #ifndef A_H
    

    Is A_H defined? No! Then continue:

    #define A_H
    

    Ok now A_H is defined. Continue:

    class A { int a; };
    

    This is not something for preprocessor, so just leave it be. Continue:

    #endif
    

    The previous if finished here. Continue:

    // From #include "b.h"
    

    Comment. Ignore! Continue:

    #ifndef B_H
    

    Is B_H defined? No! Then continue:

    #define B_H
    

    Ok now B_H is defined. Continue:

    #ifndef A_H          // From
    

    Is A_H defined? YES! Then ignore until corresponding #endif:

    #define A_H          // #include "a.h"
    

    Ignore

    class A { int a; };  // inside
    

    Ignore

    #endif               // "b.h"
    

    The previous if finished here. Continue:

    class B { int b; };
    

    This is not something for preprocessor, so just leave it be. Continue:

    #endif
    

    The previous if finished here.

    That is, after the preprocessor is done with the file, this is what the compiler sees:

    main.cpp
    class A { int a; };
    class B { int b; };
    

    So as you can see, anything that can get #included in the same file twice, whether directly or indirectly needs to be guarded. Since .h files are always very likely to be included twice, it is good if you guard ALL your .h files.

    P.S. Note that you also have circular #includes. Imagine the preprocessor copy-pasting the code of Physics.h into GameObject.h which sees there is an #include "GameObject.h" which means copy GameObject.h into itself. When you copy, you again get #include "Pysics.h" and you are stuck in a loop forever. Compilers prevent that, but that means your #includes are half-done.

    Before saying how to fix this, you should know another thing.

    If you have:

    #include "b.h"
    
    class A
    {
        B b;
    };
    

    Then the compiler needs to know everything about b, most importantly, what variables it has etc so that it would know how many bytes it should put in place of b in A.

    However, if you have:

    class A
    {
        B *b;
    };
    

    Then the compiler doesn't really need to know anything about B (since pointers, regardless of the type have the same size). The only thing it needs to know about B is that it exists!

    So you do something called "forward declaration":

    class B;  // This line just says B exists
    
    class A
    {
        B *b;
    };
    

    This is very similar to many other things you do in header files such as:

    int function(int x);  // This is forward declaration
    
    class A
    {
    public:
        void do_something(); // This is forward declaration
    }
    
    0 讨论(0)
提交回复
热议问题