|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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"); } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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... |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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.) |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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(); } |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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; |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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; } |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On 3 Jan, 21:08, Pavel Shved <Pavel.Sh...@gmail.com> wrote:
> [stuff] Sorry. :-( I wish this place had a delete button :-( |
|
![]() |
| Outils de la discussion | |
|
|