473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

query regarding namespaces

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?

Dec 12 '06 #1
9 1114
<sa*****@yahoo.co.inwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
:
: #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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Dec 12 '06 #2
>
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... :-(

Dec 12 '06 #3
On Dec 12, 8:34 am, sam_...@yahoo.co.in wrote:
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,
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.

--
Erik Wikström

Dec 12 '06 #4
er****@student.chalmers.se wrote:
>
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::

--
Ian Collins.
Dec 12 '06 #5
>
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.

Dec 12 '06 #6

sa*****@yahoo.co.in wrote:
>
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

Dec 12 '06 #7
<sa*****@yahoo.co.inwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
: >
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Dec 12 '06 #8
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

sa*****@yahoo.co.in wrote:
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?
Dec 13 '06 #9
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;
}
Ian Collins wrote:
er****@student.chalmers.se wrote:

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::

--
Ian Collins.
Dec 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
0
by: David | last post by:
Hi, I have a problem about my ASP .NET App(C# .NET) to query SMS2003 via WMI. My web server is IIS 6.0 on Win2003 Svr SP1(Domain Controller-AD + SMS2003 SP1). In my btn_cliclk method, my...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
0
by: Terrance | last post by:
Hello all, can anyone tell me if there's any type of resource(s) that gives a listing of all the namespaces (e.g. System.Data or System.String) that Visual Studio offers and a definition or...
7
by: stig | last post by:
hi. coming from postgresql, i am used to textual references to most of the things i do with the database. i feel a little lost with all the graphical. i have few questions regarding MS SQL 2000...
3
by: 0to60 | last post by:
Please help! I'm using the following code to get an XML doc: string str = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison"; System.Net.HttpWebRequest request =...
7
by: Kurt | last post by:
Hi. I have a c++ project that is basically a set of implementation hiding classes, so that we can convert a static lib with a lot of dependancies into a dynamic lib with less dependancies, so that...
1
by: silpa | last post by:
Hi, I have an SQL query like this select distinct t1.prodID from Table1 t1 left join Table2 t2 on t2.prodID = t1.prodID left join Table3 t3 on t3.serialno = t2.Id and t3.Qty = 0 ...
8
by: Goran | last post by:
Hi all, I have a question regarding operator <<. A lib of mine contains a class with an overloaded operator << as NON- class member. This would look like: #include <iostream> #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.