I have a header file MyNameSpace.h where i use namespace as under:
namespace NameSpace1
{
string first = "First";
...
}
namespace NameSpace2
{
string top = "Top";
}
But when i use the namespace object in my other classes including the header file. I got Duplicate symbol error as NameSpace1::first. What exactly it means and how to resolve this solution.
You shouldn’t define globals in headers, you need to tell the compiler it’s defined elsewhere with the
externkeyword. Otherwise the compiler tries to define the variable in every source file that includes the header.Eg. in MyNameSpace.h you do:
Then you’ll do this in MyNameSpace.cpp: