query regarding namespaces

S

sam_cit

Hi everyone,

I have the following piece of code,

#include <stdio.h>
#include "sample1.h"

int x = 10;

namespace first
{
int x = 5;
}

//using namespace first;

int main()
{
using namespace first;
printf("%d\n",x);
return(0);
}

when i compile i get an error saying 'x' : ambiguous symbol, my
requirement is to make sure main() uses variables in first namespace
rather than global namespaces, can anyone help me regarding this?
 
I

Ivan Vecerina

:
: #include <stdio.h>
: #include "sample1.h"
:
: int x = 10;
:
: namespace first
: {
: int x = 5;
: }
:
: //using namespace first;
:
: int main()
: {
: using namespace first;
: printf("%d\n",x);
: return(0);
: }
:
: when i compile i get an error saying 'x' : ambiguous symbol, my
: requirement is to make sure main() uses variables in first namespace
: rather than global namespaces, can anyone help me regarding this?

You can do it for a single identifier (e.g. first::x) by adding the
following declaration in main:
using first::x; // = like declaring a local x, will hide others

You cannot do the same for a whole namespace. (And it would not be
a good idea if it were possible, in my opinion, as the meaning of
the code could accidentally change in subtle ways during
maintenance...).


hth --Ivan
 
S

sam_cit

You can do it for a single identifier (e.g. first::x) by adding the
following declaration in main:
using first::x; // = like declaring a local x, will hide others

You cannot do the same for a whole namespace. (And it would not be
a good idea if it were possible, in my opinion, as the meaning of
the code could accidentally change in subtle ways during
maintenance...).

Well, i'm able to use the entire namespace as long as the identifer
used in the name are not global and they are within many namespaces,

#include <stdio.h>

namespace second
{
int x = 10;
}

namespace first
{
int x = 5;
}

int main()
{
using namespace first;
printf("%d\n",x);
return(0);
}

i wonder why it has been made as a limitation when the identifer is
global, after all global is yet another namespace... :-(
 
E

eriwik

Well, i'm able to use the entire namespace as long as the identifer
used in the name are not global and they are within many namespaces,

Yes, because now there is no collision, there is only one possible x.
#include <stdio.h>

namespace second
{
int x = 10;

}namespace first
{
int x = 5;

}int main()
{
using namespace first;
printf("%d\n",x);
return(0);

}

i wonder why it has been made as a limitation when the identifer is
global, after all global is yet another namespace... :-(

Say that you in one of your files include map, string, iostream, vector
and a few more and then goes using namespace std, imagine the chaos
that might happen if there in one of those files existed a function or
variable or such with the same name as one of yours, and without
warning the compiler started to use that one instead of yours. At the
very least it should warn you about it.
 
I

Ian Collins

Say that you in one of your files include map, string, iostream, vector
and a few more and then goes using namespace std, imagine the chaos
that might happen if there in one of those files existed a function or
variable or such with the same name as one of yours, and without
warning the compiler started to use that one instead of yours. At the
very least it should warn you about it.

It wouldn't, your variable would hide the one in std::
 
S

sam_cit

Say that you in one of your files include map, string, iostream, vector
and a few more and then goes using namespace std, imagine the chaos
that might happen if there in one of those files existed a function or
variable or such with the same name as one of yours, and without
warning the compiler started to use that one instead of yours. At the
very least it should warn you about it.

--

But when i clearly mention that the compiler should use the namespace
that i had specified instead of global one, in the sense, i want the
compiler to look in the local namespace first for any references and
if there are no match, search in global namespaces.
 
P

pavan734

But when i clearly mention that the compiler should use the namespace
that i had specified instead of global one, in the sense, i want the
compiler to look in the local namespace first for any references and
if there are no match, search in global namespaces.

Yes the first sentence is right, you have said the compiler to use
namespace "first" but you have not said to not use the global namespace
 
I

Ivan Vecerina

: >
: > Say that you in one of your files include map, string, iostream,
vector
: > and a few more and then goes using namespace std, imagine the chaos
: > that might happen if there in one of those files existed a function
or
: > variable or such with the same name as one of yours, and without
: > warning the compiler started to use that one instead of yours. At
the
: > very least it should warn you about it.
:
: But when i clearly mention that the compiler should use the namespace
: that i had specified instead of global one, in the sense, i want the
: compiler to look in the local namespace first for any references and
: if there are no match, search in global namespaces.

You have so many options already:
- why don't you put your function in the namespace 'first' if it
needs to use "first"'s contents before anything else ?
- why do you have that many collisions, are you abusing the
global namespace, or abusing "using namespace" directives?

I do not see how what you are asking for would help write more
maintainable code, or allow us to accomplish new things.

Is there a real-life problem that you are trying to solve ?


Regards,
Ivan
 
M

Madkour

Hi Sam,
Your code will not work because namespace is not used properly.
The idea of namespaces is to ensure that variable names are unique, so
the compiler knows which one to call.

In your example, this is what happens.
1. x = 10 is declared in the global namespace and is avilable to all
functions in the programme.

2. you declare x = 5 in the first namespace wich is fine. It's
avialable to all functions, code, etc. using that namespace.

3. (This is where the problem begins) you tell the compiler to use
namespace "first"; so it brings all the variables declared in that
namespace into scope.

4. you tell the compiler to use variable "x". But wait! Which "x" the
compiler asks. There is "x" from the global namespace that is still in
scope, and x from "first" namespace which you told the compiler to
bring into scope in step 3.

Step 3 does not over shadow / hide the global namespace (I doubt it
would hide any namespace global or not). It just brings the symbols
(variables and what not) of that namespace into scope.

Your code will work in one of the following cases.

1. Remove the "using namespace first;" in which case it will use x=10.
2. Fully qualify the x you want to use first::x in which case x=5.

Good luck and keep experimenting.

Madkour
 
M

Madkour

Hi Ian,
I'm not sure about this Ian. This type of precidence works in the case
of a local variable to a function, class, loop or condition statment
hiding another variable (with an identical symbol / name) in a more
global scope.
Ultimatly, completely global variables (those not nested in any
function, class, loop or condition statment) are most likly to be
hidden by others. This however will only occur in the same namespace. I
don't think (but not sure though) if the same would hold true to
different namespaces.
I tried the following code and it didn't work.

#include <cstdlib>
#include <iostream>
#include <stdio.h>
namespace first
{
int x = 5;
}

namespace second
{
int x = 10;
}

int main()
{
using namespace first;
printf("%d\n",x); // should work.

using namespace second; // should hide the original x...but namespaces
do not work like that.
printf("%d\n",x); // This generates an error since two variables
titled x are in scope (or in the symbol table in tech. lingo)

system("PAUSE");
return EXIT_SUCCESS;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top