There was a question about the description namespace s. I do not understand how they should be described in the header file so that when connecting this header file you can directly refer to the namespace identifier a.

For example:

 namespace whizbangStartData { float wSD_speed = static_cast<float>(0.2); const int wSD_UpX = 966, wSD_UpY = 303; const int wSD_RightX = 1035, wSD_RightY = 303; const int wSD_DownX = 1014, wSD_DownY = 303; const int wSD_LeftX = 987, wSD_LeftY = 303; const int wSD_Width = 20, wSD_Height = 20; } namespace wsd = whizbangStartData; 

Mistake:

 error LNK2005: "float whizbangStartData::wSD_speed" (?wSD_speed@whizbangStartData@@3MA) already defined in BattleCity.obj 
  • give an example of code to make it clearer - ampawd
  • @ampawd SomeClass.h #include "OtherClass.h" someNamespace :: someData How should the PI someNamespace be described in the OtherClass.h file or maybe cpp? - Sergey
  • oh my god insert this code into the question and not into the comment so that you can read it all humanely - ampawd
  • Yes, there are 4 words, what is not clear? - Sergey
  • what's the problem ?? In the header file, write the namespace someNamespace { int someData = 123; } namespace someNamespace { int someData = 123; } for example - ampawd

2 answers 2

As should be known, a line like #include <filename> causes the contents of the filename to be inserted instead of this line. Ie, connecting the file to any module X we get:

 namespace whizbangStartData { float wSD_speed = static_cast<float>(0.2); const int wSD_UpX = 966, wSD_UpY = 303; ... } // 褋芯写械褉卸懈屑芯械 屑芯写褍谢褟 X 

Separate modules (translation units) are compiled independently of each other. And when there are more than one modules that include the mentioned file, we get the names wSD_speed , wSD_UpX , etc. are present in each module. At the same time, internal constants implicitly use internal linking (i.e., they are not visible from another module, unless they are explicitly marked as extern ) and therefore the linker sees no problems with them when combining several modules. Another thing is variable. wSD_speed not a constant and its presence in the namespace makes it visible to other modules (i.e., implicit extern assumed for such variables). It turns out that in the whizbangStartData::wSD_speed process, when using whizbangStartData::wSD_speed in the code, it becomes unclear what kind of memory area is actually used and the linker displays the corresponding message.

There are several ways to eliminate the layout error:

  • Declare a variable in the header as extern float wSD_speed; , and define only in one of the modules.

  • Add static before float . This will make the variable invisible from outside the module, i.e. each module will have its own wSD_speed instance.

It is doubtful that the second option would be appropriate, but nevertheless, it solves the problem of layout.

    The main problem is that you define a variable , not a constant, in the header file. The result is that the rule of one definition is violated: in each of the object files there is the same name, and the linker just goes crazy, not understanding what you want from it.