Repeated Multiple Definition Errors from including same header in multiple cpps

后端 未结 9 1655
南方客
南方客 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:01

    While most of the other answers are correct as to why you are seeing multiple definitions, the terminology is imprecise. Understanding declaration vs. definition is the key to your problem.

    A declaration announces the existence of an item but does not cause instantiation. Hence the extern statements are declarations - not definitions.

    A definition creates an instance of the defined item. Hence if you have a definition in a header it is instantiated in each .cpp file, resulting in the multiple definitions. Definitions are also declarations - i.e. no separate declaration is needed if for instance the scope of the item is limited to one .cpp file.

    Note: the use of the word instantiation here really only applies to data items.

提交回复
热议问题