PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Namespaces
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Namespaces

Réponse
 
LinkBack Outils de la discussion
Vieux 26/12/2007, 16h42   #1
Nikos Hatzigiannakis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Namespaces

I cannot understand why the following code does not work. The compiler
displays the error
'x' undeclared (first use this function) in the sentance after the usung
namespace second; statement.
any will be appreciated .

#include <iostream>
using namespace std;

namespace first
{
int x = 5;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 2.7183;
}

int main ()
{
using namespace first;
cout << x << endl;
cout << y << endl;
using namespace second;
cout << x << endl;
cout << y << endl;
system("pause");
}


  Réponse avec citation
Vieux 26/12/2007, 17h05   #2
Rahul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Namespaces

On Dec 26, 9:42 pm, "Nikos Hatzigiannakis" <y...@aegean.gr> wrote:
> I cannot understand why the following code does not work. The compiler
> displays the error
> 'x' undeclared (first use this function) in the sentance after the usung
> namespace second; statement.
> any will be appreciated .
>
> #include <iostream>
> using namespace std;
>
> namespace first
> {
> int x = 5;
> int y = 10;
>
> }
>
> namespace second
> {
> double x = 3.1416;
> double y = 2.7183;
>
> }
>
> int main ()
> {
> using namespace first;
> cout << x << endl;
> cout << y << endl;
> using namespace second;
> cout << x << endl;
> cout << y << endl;
> system("pause");
>
> }


well i just get ambiguous symbol after using the second namespace...
but cout<<second::x<<endl; works...
  Réponse avec citation
Vieux 26/12/2007, 17h07   #3
David Côme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Namespaces

On Wed, 26 Dec 2007 17:42:41 +0100, Nikos Hatzigiannakis <ypai@aegean.gr>
wrote:

> I cannot understand why the following code does not work. The compiler
> displays the error
> 'x' undeclared (first use this function) in the sentance after the usung
> namespace second; statement.
> any will be appreciated .
>
> #include <iostream>
> using namespace std;
>
> namespace first
> {
> int x = 5;
> int y = 10;
> }
>
> namespace second
> {
> double x = 3.1416;
> double y = 2.7183;
> }
>
> int main ()
> {
> using namespace first;
> cout << x << endl;
> cout << y << endl;
> using namespace second;
> cout << x << endl;
> cout << y << endl;
> system("pause");
> }
>
>

On Wed, 26 Dec 2007 17:42:41 +0100, Nikos Hatzigiannakis <ypai@aegean.gr>
wrote:

> I cannot understand why the following code does not work. The compiler
> displays the error
> 'x' undeclared (first use this function) in the sentance after the usung
> namespace second; statement.
> any will be appreciated .
>
> #include <iostream>
> using namespace std;
>
> namespace first
> {
> int x = 5;
> int y = 10;
> }
>
> namespace second
> {
> double x = 3.1416;
> double y = 2.7183;
> }
>
> int main ()
> {
> using namespace first;
> cout << x << endl;
> cout << y << endl;
> using namespace second;
> cout << x << endl;
> cout << y << endl;
> system("pause");
> }
>
>

When you use the second namepsace by the instruction "using namespace
second;" , the compiler don't know which x and y he should use.
Use first::x and second::y ? Use second::x and fisrt::y ? ....
It's Ambiguous. You must leave the ambiguity by first:: or second:: before
x and y after tou have written "using namespace second;"

So it become :

int main ()
{
using namespace first;
cout << x << endl;
cout << y << endl;
using namespace second;
cout << second::x << endl;
cout << second::y << endl;
system("pause");
}
However if the the second namespace have a member named z, you could do
cout << z << endl without ambiguity

(When i compile your code,i've ot the same error as you.)
  Réponse avec citation
Vieux 26/12/2007, 17h09   #4
Rahul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Namespaces

On Dec 26, 10:07 pm, David Côme <davidc...@wanadoo.fr> wrote:
> On Wed, 26 Dec 2007 17:42:41 +0100, Nikos Hatzigiannakis <y...@aegean.gr>
> wrote:
>
>
>
> > I cannot understand why the following code does not work. The compiler
> > displays the error
> > 'x' undeclared (first use this function) in the sentance after the usung
> > namespace second; statement.
> > any will be appreciated .

>
> > #include <iostream>
> > using namespace std;

>
> > namespace first
> > {
> > int x = 5;
> > int y = 10;
> > }

>
> > namespace second
> > {
> > double x = 3.1416;
> > double y = 2.7183;
> > }

>
> > int main ()
> > {
> > using namespace first;
> > cout << x << endl;
> > cout << y << endl;
> > using namespace second;
> > cout << x << endl;
> > cout << y << endl;
> > system("pause");
> > }

>
> On Wed, 26 Dec 2007 17:42:41 +0100, Nikos Hatzigiannakis <y...@aegean.gr>
> wrote:
>
>
>
> > I cannot understand why the following code does not work. The compiler
> > displays the error
> > 'x' undeclared (first use this function) in the sentance after the usung
> > namespace second; statement.
> > any will be appreciated .

>
> > #include <iostream>
> > using namespace std;

>
> > namespace first
> > {
> > int x = 5;
> > int y = 10;
> > }

>
> > namespace second
> > {
> > double x = 3.1416;
> > double y = 2.7183;
> > }

>
> > int main ()
> > {
> > using namespace first;
> > cout << x << endl;
> > cout << y << endl;
> > using namespace second;
> > cout << x << endl;
> > cout << y << endl;
> > system("pause");
> > }

>
> When you use the second namepsace by the instruction "using namespace
> second;" , the compiler don't know which x and y he should use.
> Use first::x and second::y ? Use second::x and fisrt::y ? ....
> It's Ambiguous. You must leave the ambiguity by first:: or second:: before
> x and y after tou have written "using namespace second;"
>
> So it become :
>
> int main ()
> {
> using namespace first;
> cout << x << endl;
> cout << y << endl;
> using namespace second;
> cout << second::x << endl;
> cout << second::y << endl;
> system("pause");
> }
> However if the the second namespace have a member named z, you could do
> cout << z << endl without ambiguity
>
> (When i compile your code,i've ot the same error as you.)


And is there anyway to turn off a visible namespace?
  Réponse avec citation
Vieux 26/12/2007, 18h20   #5
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Namespaces

On Dec 26, 12:09 pm, Rahul <sam_...@yahoo.co.in> wrote:
> On Dec 26, 10:07 pm, David Côme <davidc...@wanadoo.fr> wrote:
>
>
>
> > On Wed, 26 Dec 2007 17:42:41 +0100, Nikos Hatzigiannakis <y...@aegean.gr>
> > wrote:

>
> > > I cannot understand why the following code does not work. The compiler
> > > displays the error
> > > 'x' undeclared (first use this function) in the sentance after the usung
> > > namespace second; statement.
> > > any will be appreciated .

>
> > > #include <iostream>
> > > using namespace std;

>
> > > namespace first
> > > {
> > > int x = 5;
> > > int y = 10;
> > > }

>
> > > namespace second
> > > {
> > > double x = 3.1416;
> > > double y = 2.7183;
> > > }

>
> > > int main ()
> > > {
> > > using namespace first;
> > > cout << x << endl;
> > > cout << y << endl;
> > > using namespace second;
> > > cout << x << endl;
> > > cout << y << endl;
> > > system("pause");
> > > }

>
> > On Wed, 26 Dec 2007 17:42:41 +0100, Nikos Hatzigiannakis <y...@aegean.gr>
> > wrote:

>
> > > I cannot understand why the following code does not work. The compiler
> > > displays the error
> > > 'x' undeclared (first use this function) in the sentance after the usung
> > > namespace second; statement.
> > > any will be appreciated .

>
> > > #include <iostream>
> > > using namespace std;

>
> > > namespace first
> > > {
> > > int x = 5;
> > > int y = 10;
> > > }

>
> > > namespace second
> > > {
> > > double x = 3.1416;
> > > double y = 2.7183;
> > > }

>
> > > int main ()
> > > {
> > > using namespace first;
> > > cout << x << endl;
> > > cout << y << endl;
> > > using namespace second;
> > > cout << x << endl;
> > > cout << y << endl;
> > > system("pause");
> > > }

>
> > When you use the second namepsace by the instruction "using namespace
> > second;" , the compiler don't know which x and y he should use.
> > Use first::x and second::y ? Use second::x and fisrt::y ? ....
> > It's Ambiguous. You must leave the ambiguity by first:: or second:: before
> > x and y after tou have written "using namespace second;"

>
> > So it become :

>
> > int main ()
> > {
> > using namespace first;
> > cout << x << endl;
> > cout << y << endl;
> > using namespace second;
> > cout << second::x << endl;
> > cout << second::y << endl;
> > system("pause");
> > }
> > However if the the second namespace have a member named z, you could do
> > cout << z << endl without ambiguity

>
> > (When i compile your code,i've ot the same error as you.)

>
> And is there anyway to turn off a visible namespace?


namepaces aren't turned on or off.
You either provide access or you don't.
A far better system than an on/off toggle is scopes:

#include <iostream>

namespace test
{
int n;
}

void foo()
{
using namespace test;
std:: cout << "n = " << n << std::endl;
}

int main()
{
test::n = 0; // ok
// n = 99; // error
{ // anonymous scope
using namespace test;
n = 99;
}
// std:: cout << "n = " << n << std::endl; // error
foo();
}
  Réponse avec citation
Vieux 27/12/2007, 02h31   #6
siddhu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Namespaces

On Dec 26, 11:42 am, "Nikos Hatzigiannakis" <y...@aegean.gr> wrote:
> I cannot understand why the following code does not work. The compiler
> displays the error
> 'x' undeclared (first use this function) in the sentance after the usung
> namespace second; statement.
> any will be appreciated .
>
> #include <iostream>
> using namespace std;
>
> namespace first
> {
> int x = 5;
> int y = 10;
>
> }
>
> namespace second
> {
> double x = 3.1416;
> double y = 2.7183;
>
> }
>
> int main ()
> {
> using namespace first;
> cout << x << endl;
> cout << y << endl;
> using namespace second;
> cout << x << endl;
> cout << y << endl;
> system("pause");
>
> }


Do not use using clause. it increases the chances of name conflicts.
Use fully qualified names.
e.g.
first::x;
second::x;
  Réponse avec citation
Vieux 03/01/2008, 16h08   #7
Pavel Shved
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Namespaces

On 26 ÄÅË 2007, 22:09, Rahul <sam_...@yahoo.co.in> wrote:

> And is there anyway to turn off a visible namespace?


Just enclose using clause to a `compound operator':

{
using namespace first;
cout << x << endl;
cout << y << endl;
}
{
using namespace second;
cout << x << endl;
cout << y << endl;
}
  Réponse avec citation
Vieux 03/01/2008, 16h13   #8
Pavel Shved
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Namespaces

On 3 Jan, 21:08, Pavel Shved <Pavel.Sh...@gmail.com> wrote:
> [stuff]


Sorry. :-( I wish this place had a delete button :-(
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h25.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,74026 seconds with 16 queries