Repeated Multiple Definition Errors from including same header in multiple cpps

后端 未结 9 1680
南方客
南方客 2020-11-27 06:12

So, no matter what I seem to do, I cannot seem to avoid having Dev C++ spew out numerous Multiple Definition errors as a result of me including the same header file in multi

9条回答
  •  盖世英雄少女心
    2020-11-27 07:08

    You need to define your variables as extern in the header file, and then define them in a cpp file as well. i.e.:

    extern MYSTRUCT Job_Grunt;
    

    in your header file, and then in a cpp file in your project declare them normally.

    The header file is only for definitions, when you instantiate a variable in the header file it will try to instantiate it every time the header is included in your project. Using the extern directive tells the compiler that it's just a definition and that the instantiation is done somewhere else.

提交回复
热议问题