MyNamespace.h:
namespace MyNamespace{
int a
}
MyNamespace.cpp: some function that uses a
main.cpp
#include "MyNamespace.h"
main.obj : error LNK2005: "class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > FileNamespace::m_rootDirectoryPath" (?m_rootDirectoryPath@FileNamespace@@3V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@A) already defined in FileNamespace.obj 1>main.obj : error LNK2005: "struct FileNamespace::FileTree FileNamespace::m_dataFileTree" (?m_dataFileTree@FileNamespace@@3UFileTree@1@A) already defined in FileNamespace.obj
You are defining a global variable (with external linkage) in multiple translation units, which results in duplicate definition errors (since you are violating the ODR).
What you should do, instead, is to declare it in the header with an
externdeclarationand define it in a single .cpp file (probably in
MyNamespace.cpp)This way, the compiler will create only one instance of this variable in a single object module, and the linker will link all the references to it made in other object modules to this single instance.
It may help you to understand the problem noticing that this is the exact equivalent of declaring functions in headers (writing there only the prototype) and defining them in a single
.cpp.