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.
Related
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have searched this question in Google. I found some related question in stackoverflow.com and quora.com but i am still not clear about this two topics. Everyone says that we use #include<iostream> for input/output operation. Now, we take input using cin and print output using cout that means this two should be defined in #include<iostream>. But without using using namespace stdwe still can't take any input nor can print something on console. So, my questions are-
Where is cin and cout actually declared and defined? Is it in #include<iostream>or in namespace std?
If in #include<iostream>why should we use using namespace std?
If in namespace std why should we use #include<iostream?
After reading some article on the web and watching some videos on YouTube, I'm assuming cout and cin is defined in namespace std and the namespace std doesn't make any sense alone because it is defined in #include<iostream>. That's why we need to use them both. (Just my thought let me know if I am right or not.)
The purpose of this question is to be clear about this two facts. If you can help it would be great.
cin and cout are defined in the header iostream and in the namespace std. These concepts are orthogonal. iostream is a file name and std is a namespace used by the source code of that file.
As a way of keeping things organized, c++ provides namespaces. All of the standard library is defined within the namespace called std. Other libraries you might write or include may use their own namespace. For example, you might include a physics library in your project which wants to define the concept of algebraic vectors. By using it's own namespace (let's called if physlib) it can differentiate between it's vector (physlib::vector) and the standard vector (std::vector). Namespaces can also be nested to help organize large projects. For example, time keeping parts of the standard library are in std::chrono and file system related components are in std::filesystem.
The preferred way of using cin and cout is as following. :
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
return 0;
}
The statement using namespace std is simply an instruction to look in the namespace std by default. It allows you to omit the std:: part of using standard library components. It's generally regarded as a bad idea to used using namespace std.
Why should we use #include <iostream>
To bring the standard library's I/O functionality into our program.
while we are using using namespace std?
This allows us to use that functionality without writing std:: each time we do.
This is unrelated to the previous step. Writing only using namespace std does not bring I/O functionality into your program, and writing only #include <iostream> does not allow us to use that functionality without writing its components' names out in full (including the std:: prefix).
The #include directive determines what we can use;
The using namespace declaration determines how we can use it.
Perfectly fine:
#include <iostream>
int main()
{
std::cout << "Hello world!\n";
}
Also valid:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!\n";
}
Not valid:
int main()
{
std::cout << "Hello world!\n";
}
And neither is this:
using namespace std;
int main()
{
std::cout << "Hello world!\n";
}
or this:
using namespace std;
int main()
{
cout << "Hello world!\n";
}
#include <iostrem> tells the compiler to pull in the contents of the header iostream. That header provides, among other things, declarations of the objects std::cin and std::cout. So the basic "Hello, world" program looks like this:
#include <iostream>
int main() {
std::cout << "Hello, world\n";
return 0;
}
Similarly, if you want to use std::vector, you tell the compiler about it with #include <vector>. Same thing for the rest of the standard library: whatever it is that you want to use, find out which header declares it and #include that header.
using namespace std; doesn't define any names for you. It tells the compiler to pretend that any names that have been defined in the namespace std are also defined in the scope where using namespace std; occurs. So that means that instead of writing std::cout you can write cout, and the compiler will figure out that you meant std::cout. Unless, of course, you've written something yourself with the name cout (or any other name that's in std and declared in a header that you've #included), in which case that using declaration makes the use of the name ambiguous. There is no good reason to write using namespace std;. Just use the right names for things: std::cout is clear and unambiguous.
Where is cin and cout actually declared and defined? Is it in #include<iostream> or in namespace std?
It's not or. cin and cout are declared in the iostream header file within the namespace std.
If in #include< iostream> why should we use using namespace std?
You shouldn't. Either fully qualify the global variables like std::cin or std::cout. There's a number of reasons why you shouldn't use using namespace std; (at least in header files).
If in namespace std why should we use #include<iostream>?
Because you need the declarations to compile your code.
iostream is part of the standard library, here is what the "iostream.h" file would look like if you were to implement it yourself:
namespace std{
// code about cin
extern ostream cin;
// code about cout
extern ostream cout;
}
(see c++ STL cout source code)
Where is cin and cout actually declared and defined? Is it in #include <iostream> or in namespace std?
So cin/cout are declared and define in the standard library under the
namespace std. This means that if you want to use it in your code you can do :
int main()
{
std::cout << "Hello";
}
If in #include<iostream> why should we use using namespace std?
You don't need using namespace std, you could call std::cout everywhere instead of cout, but you can see how this can make your code verbose and annoying to type.
using namespace std in your whole file is actually discouraged as it could have unintended effect, usually you could use only
using std::cout;
using std::cin;
where needed.
The reason the using is needed (or the need for explicitly writing std:cout , is imagine you have the following code:
#include <iostream>
namespace mycoolnamespace
{
class foo {
public:
foo& operator<< (const std::string& echo)
{
// do stuff
}
};
foo cout; // created a variable called cout
}
int main()
{
cout << "Hello";
// ^^^^ compiler can't know if you meant to use std::cout or mycoolnamespace::cout here!
// Potential fix:
std::cout << "Hello"
}
If in namespace std why should we use #include<iostream>?
By default not all function/classes from the standard library are included in your program, otherwise a simple "Hello world" program would give a lot of work to the compiler since it would need to parse all the existing classes/functions in the entire standard library. #include<iostream> tells the compiler that you need the functions/classes available in the standard library's iostream 'file'
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;
Anyone can help me to understand this statement found in chapter 3 (Library Types) by Stanley Lipmann?
"Using an unqualified version of a namespace name without a using declaration is an error, although some compilers may fail to detect this error"
I'm having such hard time understanding the semantics of his sentence (english).
Is he trying to say something like the below scenario?
int main() {
xx::yy
}
where xx is a namespace not defined using the "using" statement and yy is a member?
Example:
cout is a name of the std namespace. The unqualified name is cout. The qualified name is std::cout. It is an error to use the unqualified name(cout) without a using declaration beforehand. You can use either one of the two following declarations:
// This brings in the entire std namespace
using namespace std;
OR
// This only brings in cout. You would still need to qualify other names,
// such as cin, endl, etc...
using std::cout;
What he's saying is that the following code should not compile:
#include <iostream>
void foo() {
cout << "This is an error!" << endl;
}
The cout and endl names are not defined right now. They're declared as std::cout and std::endl, and in order to use them, you can do one of a few things:
#include <iostream>
void foo() {
std::cout << "This, I think, is the best way to do it." << std::endl;
}
Using the fully qualified name prevents collisions later on: you'll never have something else called std::cout.
#include <iostream>
void foo() {
using std::cout;
using std::endl;
cout << "This is pretty good." << endl;
}
Having the using statements specify the exact names you're using, and having the using statements in the function, can save some typing and makes collisions pretty unlikely.
#include <iostream>
using namespace std;
void foo() {
cout << "This works, but isn't good." << endl;
}
Importing the entire std namespace makes it pretty likely that you'll end up having a function named the same as an std function.. You might discover that as soon as you write it, or you might write your function and then later include the header file with the std version of the function, at which point your application will mysteriously break.
A namespace name is the name of a namespace.
namespace A {
}
namespace B = A;
The statement says that using a namespace name without a using declaration is an error. But that's not true: The above code is fine, still using the namespace-name A as an unqualified name.
Probably it should say the following, to convey its meaning
"Using an unqualified version of a namespace member name without a using declaration
outside the scope of the namespace is an error, although some compilers may fail to
detect this error"
Mentioning the scope is important. The following, for example, is fine too, even though it uses the unqualified version of a namespace member name
namespace A {
int x;
int &y = x; // x is an unqualified name
}
Books should be careful to try and not use slippery language. And even outside the scope of the namespace, the above sentence is not entirely correct because you can also extend the scope of x by a using directive. Using declarations aren't the only way to name a namespace member outside the namespace using an unqualified name.
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
If I remove the 2nd statement,the build will fail.
Why is it necessary?
Because cout and endl are contained inside the std namespace.
You could remove the using namespace std line and put instead std::cout and std::endl.
Here is an example that should make namespaces clear:
Stuff.h:
namespace Peanuts
{
struct Nut
{
};
}
namespace Hardware
{
struct Nut
{
};
}
When you do something like using namespace Hardware you can use Nut without specifying the namespace explicitly. For any source that uses either of these classes, they need to 1) Include the header and 2) specify the namespace of the class or put a using directive.
The point of namespaces are for grouping and also to avoid namespace collisions.
Edit for your question about why you need #include :
#include <iostream> includes the source for cout and endl. That source is inside the namespace called std which is inside iostream.
cout is part of the namespace std. Now if you were to use "std::cout" and delete the second line, then it will compile.
Yes cout and cerr are defined in isotream, but as std::cout and std::cerr
The reason for this is that you can happily use common words like min or max without worryign that some standard library has already sued them, simply write std::min and std::max. This is no different from the old way of putting eg 'afx' in front of all the ATL library function.
The 'using' statement is because people complained about the extra typing, so if you put 'using std' it assumes you meant std:: in front of everything that comes from standard.
The only problem is if you have a library called mystuff that also has a min() or max(). If use use std::min() and mystuff::min() there is no problem, but if you put 'using std' and 'using mystuff' you are back to the same problem you had in 'c'
ps. as a rule it is good practice to put std::cout just to make it clear to people that this is the regualr standard version and not some local version of cout you have created.