Namespaces

  • Thread starter Nikos Hatzigiannakis
  • Start date
N

Nikos Hatzigiannakis

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

Rahul

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 help 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...
 
D

David Côme

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 help 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");
}
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 help 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

Rahul

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?
 
S

Salt_Peter

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();
}
 
S

siddhu

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 help 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;
 
P

Pavel Shved

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;
}
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top