Variables in (and out) of namespaces

K

K Campbell

Say I have the code in a program below:

namespace c_name
{
int j;
}

int main(int argc, char* argv[])
{
using namespace std;
int j;
cout<<"Hello, World!"<<endl;
using namespace c_name;
j = 5;
}

I was wondering how I could access j (the not the one in c_name).
Any tips?
Kieran
 
V

Victor Bazarov

K Campbell said:
Say I have the code in a program below:

namespace c_name
{
int j;
}

int main(int argc, char* argv[])
{
using namespace std;
int j;
cout<<"Hello, World!"<<endl;
using namespace c_name;
j = 5;
}

I was wondering how I could access j (the not the one in c_name).
Any tips?

There is no way. A tip: don't use 'using', use 'c_name::'.

V
 
S

Siemel Naran

K Campbell said:
namespace c_name
{
int j;
}

int main(int argc, char* argv[])
{
using namespace std;
int j;
cout<<"Hello, World!"<<endl;
using namespace c_name;
j = 5;
}

I was wondering how I could access j (the not the one in c_name).
Any tips?

Does ::j work?
 
V

Victor Bazarov

Siemel Naran said:
K Campbell said:
namespace c_name
{
int j;
}

int main(int argc, char* argv[])
{
using namespace std;
int j;
cout<<"Hello, World!"<<endl;
using namespace c_name;
j = 5;
}

I was wondering how I could access j (the not the one in c_name).
Any tips?

Does ::j work?

Why would it? There is no j in the global namespace.

V
 
S

Siemel Naran

Victor Bazarov said:
int main(int argc, char* argv[])
{
using namespace std;
int j;
cout<<"Hello, World!"<<endl;
using namespace c_name;
j = 5;
}

I was wondering how I could access j (the not the one in c_name).
Any tips?

Does ::j work?

Why would it? There is no j in the global namespace.

Yes, I tried it and it gives a compile error.
 

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

Members online

Forum statistics

Threads
474,181
Messages
2,570,971
Members
47,537
Latest member
BellCorone

Latest Threads

Top