Using namespace scope - c++

I tried the following piece of code. When i compile, i get error that there there are ambiguous instances of first_var, whereas i have introduced
using namespace second_space before the last cout
I guess this is because the last cout is using both the namespaces. There is no override concept for namespaces ? Is there anyway a namespace scope can be ended or it continues from the using namespace point to the end of file?
#include<iostream.h>
namespace first_space{
int first_var;
}
namespace second_space{
int first_var = 1;
}
int main()
{
cout<<"Hello World"<<endl;
cout<<"First Namespace Variable using namespace identifier:"<<first_space::first_var<<endl;
using namespace first_space;
cout<<"First Namespace Variable using using identifier:"<<first_var<<endl;
using namespace second_space;
cout<<"Second Namespace Variable using using identifier:"<<first_var<<endl;
}
Edit 1:
I tried something like this below. Declared a variable with same name inside the main, assigned a value 1 to it and then used using namespace below that. But I see that, the value of first_var is printed as 1 in the last two cout.
There is no ambiguity here. So the namespace didn't have any effect? Why is it so?
#include<iostream.h>
namespace first_space{
int first_var;
}
namespace second_space{
int first_var = 1;
}
int main()
{
int first_var =1 ;
using namespace first_space;
cout<<"Hello World"<<endl;
cout<<"First Namespace Variable using namespace identifier:"<<first_space::first_var<<endl;
cout<<"First Namespace Variable using using identifier:"<<first_var<<endl;
// using namespace second_space;
cout<<"Second Namespace Variable using using identifier:"<<first_var<<endl;
}
Output:
Hello World
First Namespace Variable using namespace identifier:0
First Namespace Variable using using identifier:1
Second Namespace Variable using using identifier:1

Yes you are correct, after the 2nd using statement, the variable first_var is now ambiguous because both of the two namespaces are valid and of equal precedence as far as name lookup is concerned.
The two workarounds are
a) add braces to enforce an anonymous scope (live demo)
{
using namespace first_space;
cout << "First Namespace Variable using using identifier:" << first_var << endl;
}
{
using namespace second_space;
cout << "Second Namespace Variable using using identifier:" << first_var << endl;
}
b) drop the using keyword and use the namespace scope directly
cout << "First Namespace Variable using using identifier:" << first_space::first_var << endl;
cout << "Second Namespace Variable using using identifier:" << second_space::first_var << endl;
Personally I would go with option b. One of the main reasons you add namespaces in the first place is to avoid ambiguity issues, so polluting the current scope with a bunch of using statements undermines that.

Related

can "using namespace std;" and "std::cout" be used together?

No an error occurs when using using namespace std; and std::cout together. Can these two be used together?
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello world!";
return 0;
}
There is no problem. In this statement
std::cout << "Hello world!";
there is used the qualified name lookup of the name cout in the namespace std.
You could also write
cout << "Hello world!";
and in this case there would be used the unqualified name lookup and the name cout would be found due to the directive
using namespace std;
You could also include the using declaration like
using std::cout;
Pay attention to that you should avoid to use the using directive. It can be a reason of ambiguity. It is much better to use qualified names.
The purpose of namespace is to protect against collision. When you type in using namespace std; it turns off that protection. This lets you use cout, string, vector... without std:: resolution, but they may collide with other namespaces.
In some tutorials you may see using namespace std; They put that in there to make the examples shorter without having to type in std:: every where. But that usage is limited to short examples. For actual code it is recommended not to add using namespace std;
You can always use std::cout, std::string, std::vector etc. without worrying about collision.

Why do I need to do 'using namespace std' instead of 'using std::cout'?

everyone.
I have learnt that it is often desirable to write in my codes using std::cout instead of using namespace std in order to avoid namespace conflicts. In the following script I only use cout and if I write std:: cout instead of using namespace std; it does not work.
Can anyone please help me understand why? In general, when does std::cout not work and I am forced to use using namespace std?
#include <iostream>
#include <string>
using std::cout; //if writing here "using namespace std;" instead, the code does not work
class MyClass{
public:
string name;
MyClass (string n)
{
name=n;
cout<<"Hello "<<name;
}
};
int main()
{
MyClass MyObj("Mike");
return 0;
}
You need to add using std::string; along with using std::cout; to make it work as you're not only using cout from namespace std, string is also a member of namespace std which you are using in your code.
Your code works okay with:
using std::cout;
statement for cout, but the compiler must know the location of string (actually it's std::string) too which you're currently using. You must define:
using std::string;
When you enter:
using namespace std;
It calls the entire namespace called std which contains a variety of features added as C++ standard library and then you don't need to use prefix std:: for those functions/classes/variables of that namespace.

About std:cout in C++

Is there an error in this code:
#include <iostream>
using namespace std;
int main()
{
std:cout << "hello" "\n";
}
GCC detects no error but std:cout does not seem standard.
There's no error. I could rewrite your code to make it clearer:
#include <iostream>
using namespace std;
int main()
{
std:
cout << "hello" "\n";
}
You created a label named std. cout is used unqualified, which is okay since you have the using-directive for std above it. And you can concatenate string literals by writing them next to each other as you did. This is perfectly well-formed code that prints "hello" followed by a newline.
You're defining a label std and then you're calling cout. This is legal because you have using namespace std;
The code has an issue.
while trying to instruct the compiler to use the namespace std, we are trying to call the function cout which is defined in the scope of std.
thus the correct use of the scope resolution operator is
'std::cout '
and not
std:cout.
And others have pointed out
by writing
std:
what you do is create a label.

namespace validity in c++

I have a simple question for namespace in C++. There are errors when I compiling the following little piece of code. I don't understand why. Thanks for any help in advance!
#include <iostream>
using namespace std;
int x=100;
namespace first
{
int x=1;
}
namespace second
{
int x=2;
}
int main(){
{
using namespace first;
cout<<x<<endl;
}
{
using namespace second;
cout<<x<<endl;
}
cout<<x<<endl;
}
If I comment out the x declared in the global scope and the last statement. It works fine. But in my mind, the first x is declared in the std namespace and using namespace first and second in the main will be invalid after the the code block they are declared(so the namespace will be std again). So the above code should work. Where am I wrong?
But in my mind, the first x is declared in the std namespace
Your mind is wrong. The using namespace std makes names from std available in the global namespace, it doesn't mean names declared in the global namespace are in std.
x is declared in the global namespace.
so the namespace will be std again
No, nothing in your file is in namespace std, only the contents of <iostream> are in namespace std.
The error is that when you try to use x there are two different variables in scope, ::x and first::x that you could be referring to, so it is ambiguous. You can disambiguate with a using declaration instead of a using directive:
{
using first::x;
cout<<x<<endl;
}
This says that in that scope x refers to first::x
in this case x variable is ambiguous. The compiler can't find which x you are going to use. You can write like this.
#include <iostream>
using namespace std;
int x=100;
namespace first
{
int x=1;
}
namespace second
{
int x=2;
}
int main(){
{
cout<<first::x<<endl;
}
{
cout<<second::x<<endl;
}
cout<<x<<endl;
}
now your code will compile.
The first x is not in the std namespace. It would only be in the std namespace if defined like this:
namespace std
{
int x;
}
Because it's not in that namespace, it's matched by references to x later in the program, and such references become ambiguous when another namespace declaring x is also being used (as in using namespace first / second).

Getting error in cout Simple code

I'm using Visual Studio 2010, and I'm wondering why I'm getting an error.
The error is: cout is undefined
#include<iostream>
#include<stdio.h>
#include<conio.h>
int main()
{
cout<<"Why am I not working ??";
printf("My Name is Khan and I'm not a terrorist.");
return 0;
}
cout is in the std namespace. You either need to declare that you are using the std namespace by adding the following to your code (it is generally put just after includes), though this is generally considered bad practise for non-trivial code:
using namespace std;
Or you can qualify cout every time it is used (this is generally preferred):
std::cout << "Hello, World!" << std::endl;
cout is a global object that lives in the std namespace. You have to fully qualify the name:
std::cout << "Hello";
// ^^^^^
If you really want to omit the qualification, you could have a using declaration in main() before you use the unqualified name cout (in general, avoid putting using declarations at global namespace scope):
// ...
int main()
{
using std::cout;
// ^^^^^^^^^^^^^^^^
cout << "Why I'm not working ??";
// ...
}
Add the following before int main:
using namespace std;