how to use user-defined namespaces in another file?

Write your question here.

1
2
3
//filex.cpp
namespace xxx
{int x = 5;}


1
2
3
4
5
//file.cpp
//and here i want to use the filex.cpp namespace xxx
...
using namespace xxx;// turned out to be wrong.
...


How to make it?
You have to write namespace xx in filex.h or filex.hpp(header file), not in .cpp file. Include header file then, and it should work
should i include all the user-defined namespaces in the header files? what if i want to include something like int x; or function definitions inside a namespace?
Sorry, I misunderstand you.

Looks like it is your problem, with solution:
http://stackoverflow.com/questions/8971824/multiple-definition-of-namespace-variable-c-compilation

(tl;dr - you can't define variable in header; use extern keyword, and define var in .cpp file)
Basically, in filex.cpp you only define variables or functions. Declare them in header file, and use them in your code.
Last edited on
In your namespace you only declare variables. You cannot give them a value.
1
2
3
4
5
namespace mynspace
{
int x = 5; //wrong
int x; //correct
}


To use variables from this namespace:
 
mynspace::x = 1; 


using namespace <name of name space> would seem to totally defeat the point of namespaces to begin with.
Topic archived. No new replies allowed.