C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

query regarding namespaces

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Tue Dec 12, 2006 10:11 am    Post subject: query regarding namespaces Reply with quote



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?
Back to top
Guest






PostPosted: Tue Dec 12, 2006 10:11 am    Post subject: Re: query regarding namespaces Reply with quote



sam_cit (AT) yahoo (DOT) co.in wrote:
Quote:

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
Back to top
Ivan Vecerina
Guest





PostPosted: Tue Dec 12, 2006 10:11 am    Post subject: Re: query regarding namespaces Reply with quote



<sam_cit (AT) yahoo (DOT) co.in> wrote in message
news:1165907590.919757.286240 (AT) l12g2000cwl (DOT) googlegroups.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:Mad) by adding the
following declaration in main:
using first:Mad; // = 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
Back to top
Ian Collins
Guest





PostPosted: Tue Dec 12, 2006 10:11 am    Post subject: Re: query regarding namespaces Reply with quote

eriwik (AT) student (DOT) chalmers.se wrote:
Quote:

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.
Back to top
Guest






PostPosted: Tue Dec 12, 2006 10:11 am    Post subject: Re: query regarding namespaces Reply with quote

Quote:

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.
Back to top
Guest






PostPosted: Tue Dec 12, 2006 10:11 am    Post subject: Re: query regarding namespaces Reply with quote

Quote:

You can do it for a single identifier (e.g. first:Mad) by adding the
following declaration in main:
using first:Mad; // = 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... Sad
Back to top
eriwik@student.chalmers.s
Guest





PostPosted: Tue Dec 12, 2006 10:11 am    Post subject: Re: query regarding namespaces Reply with quote

On Dec 12, 8:34 am, sam_...@yahoo.co.in wrote:
Quote:
You can do it for a single identifier (e.g. first:Mad) by adding the
following declaration in main:
using first:Mad; // = 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.

Quote:

#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... Sad

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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.