Hi,
I have a namespace in my header file and a variable declared in it.

When I am trying to assign it to a value in my .cpp file in the format namespaceName::variableName = value , it is throwing a linker error saying it is already defined in .obj file.

Any suggestions about this

>>I have a namespace in my header file and a variable declared in it.
Is this a .h header file? If yes then you may be perhaps compiling it separately which is not required.
say I have a header file my.h:

namespace my
{
    int a;
}

and I have my main.cpp as this:

#include<iostream>
#include "my.h"  //including the header file
int main()
{
my::a=56;
std::cout<<my::a;
}

Then I should just compile/link/run the main.cpp without compiling my.h separately.

My header file structure:
.h file
namespace SWDL {
std::wstring id;
}

.cpp file

using namespace SWDL;

namespaceSWDL {
SWDL::id = some std::wstring value;
}

In this case it is throwing linker error

first of all always use the

tags while posting a c++ code.
The problem is that in your .cpp file, while assigning a value to SWDL::id, you are enclosing it within a namespace block. You cannot initialize a value in the definition. 
So the corrected code would be like this :
[B]the .h file:[/B]
[code=cpp]
namespace SWDL {
std::wstring id;
}

the .cpp file:

... 
..
...
SWDL::id = some std::wstring value;//this statement must be inside a function
...
...
...

Also, you don't need to put the whole qualifying name (namespace::member) when you have already told the compiler that you will be using the SWDL namespace.

#include<iostream>
#include "my.h"  //including the header file
int main()
{
my::a=56;
std::cout<<my::a;
}

Only a little remark but where is the return 0; ?

return 0 is not mandatory when writing C++ main function.
Read Bjarne Stroustrup ( father of C++) homepage: http://www.research.att.com/~bs/bs_faq2.html#void-main
Here is the excerpt from the page:

In C++, main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:

#include<iostream>

	int main()
	{
		std::cout << "This program returns the integer value 0\n";
	}
commented: Nice to know ! +1

I know, but some operating systems like Unix and Linux aren't liking it if your program doesn't return an exit code ...

Srry I didn't read your post carefully ...
...

I know, but some operating systems like Unix and Linux aren't liking it if your program doesn't return an exit code ...

Wrong!
The fact that I told you is been defined by the language. It is implementation independent. It is a part of C++ standard to return a zero when no explicit return statement is encountered in main().

So, no matter what OS you use, if you are programming with a Standard compiler, you can skip the return 0; assuming that the compiler will put that for you.

BTW, even I use Linux. When I don't put a return zero, my program runs flawlessly.

Edit:: I posted this post before reading the last message. So the apology has been accepted. Never mind

Member Avatar for jencas

Do you have include guards in your .h file??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.