What order do I include header files in?

前端 未结 5 1409
挽巷
挽巷 2021-02-15 11:26

I\'m new to programming and the topic of header files is sort of bogging me down after I started using a lot of them. In addition to that I\'m trying to use precompiled headers.

5条回答
  •  梦毁少年i
    2021-02-15 12:06

    C inherits class B. So, it should see the identifier B. So, include B.h here -

    #include "B.h"    // Newly added
    // Or you can forward declare class B ; 
    class C: public B
    {
    
    };
    

    D has objects of class A, B. So, include headers of A, B in the "D.h" itself.

    class D
    {
        A a;  // Should see the definition of class A
        C c;  // Should see the definition of class B
        //sfml class
    }
    

    D.cpp

    #include "A.h"
    #include "C.h"
    #include "D.h"  // Notice that A.h and C.h should definitely placed before
    

    Note that every header needs to be included in it's corresponding source file. Think of each source file independently and see whether what ever is used is earlier defined or not in the source file.

提交回复
热议问题