C++ #include guards

后端 未结 8 690
鱼传尺愫
鱼传尺愫 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:49

    You have circular references here: Physics.h includes GameObject.h which includes Physics.h. Your class Physics uses GameObject* (pointer) type so you don't need to include GameObject.h in Physics.h but just use forward declaration - instead of

    #include "GameObject.h" 
    

    put

    class GameObject;   
    

    Furthermore, put guards in each header file.

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

    Use include guards in ALL your header files. Since you are using Visual Studio you could use the #pragma once as the first preprocessor definition in all your headers.

    However I suggest to use the classical approach:

    #ifndef CLASS_NAME_H_
    #define CLASS_NAME_H_
    
    // Header code here
    
    #endif //CLASS_NAME_H_
    

    Second read about forward declaration and apply it.

    0 讨论(0)
提交回复
热议问题