I read in the C++ book -> C++ Programming: Visual Quickstart Guide that in order to use statements such as cin and cout without std:: you need to add the line using namespace std.
However at the moment I'm currently doing A-level computer science, using C++ and CBuilder, and I don't have to add that line to my program - since I'm able to use cout and cin in their own without running into any errors.
Is it not actually needed then? Or does Cbuilder configure std itself?
In short: Why am I able to skip using std despite having not declared that I'm not using it?
Some compilers/libraries are just more lax with respect to the Standard than others: according to the Standard, your program should not compile. Namespace pollution (putting names into a namespace they don't belong, in this case the global namespace) happens a lot, and leads to these and similar errors when you switch to another compiler and/or standard library.
Unless CBuilder has an option to add an implicit using namespace std; to everything it compiles (which would be horrible), the above is all anyone can tell you. There is no "configuring" happening anywhere, not sure what you might mean by that.
No, you don't need that using statement. It is an alternative to explicit namespace reference inline within your code. For instance, you can use
std::cout << "Hello world" << std::endl;
without having a using statement.
Or, you could include the using namespace std statement and then code:
cout << "Hello world" << endl;
It's a choice. Personally I prefer to omit the using statement and explicitly insert std:: where appropriate.
It depends on the compiler you use. Some compilers add it for you. But for your own goood and of course knowledge, add using namespace std; or you have to put std:: .
Related
Recently i've noticed that if I don't include the 'using namespace std' in my program and just use 'std::cout' or 'std::endl' then my program still compiles just fine? Therefore if I use the 'std::' prefix is it even necessary to include the 'using namespace std'? Does the 'std::' prefix call the using namespace std? If someone can clear this up for me it would be greatly appreciated.
In C++, :: is called the "scope resolution operator", so std::cout means "The variable cout, which can be found inside std".
The using namespace std; directive means "For any name you can't find, also try looking it up in std". Thus, if you explicitly say std::cout (and equivalent for everything else in the std namespace), then you don't need using namespace std.
As others have said, it's usually considered bad style to use using namespace std;, because a lot of the names in std are things you might want to use elsewhere, for example std::count. A compromise option is to instead say using std::cout;, which will tell the compiler to look for cout in std but not anything else.
There's debate about whether to say using std::cout or just write std::cout everywhere you need it in your main C++ file, but most people believe that you should rarely if ever use using namespace. If you're writing a header file, though, you should never use a using directive at the top level because then every file which includes yours will get it, and they might want to use different names.
When you use:
using namespace std;
You're telling the program that anything that it can't find should be in the namespace std. For larger programs, this is a problem because you might be using multiple namespaces with functions that have the same name, so it's best to use std::cout for larger programs.
I just started C++ and started doing basic text to console programs. In the book that I'm reading (Which is pretty old, I think) it uses cout to write stuff to the screen. Simple enough, right? But when I try to write cout it says that it's not defined. I searched around the internet and it said that I need std::cout or using namespace std; How does this stuff work? Explain it to me like you would to a 5 year old. I'm next level stupid.
std is the namespace which contains all functions and classes of the standard library. namespaces are used to avoid clashes of functions/classes with the same name. You can define a namespace northern_guy which contains a class cout.
std::cout << "output"; //prints to standard output
northern_guy:cout << "output"; //does what ever is defined in that namespace
(For this example you need to overload the << operator)
So you can use both functions/classes alongside in your code. That's why you should not use using namespace std;
Why we need both the "header file" and the using namespace tag for the any library function to get executed properly. For example cout will not work unless we use iostream. Also it will not work unless we use "using namespace std". My question is why do we need combination of both using namespace std as well as #include <iostream> for cout to execute successfully?
Including a library header makes the library feature visible to your program code. Without that, your program has no idea that the library even exists. This is the part that is necessary.
Writing using namespace std simply allows you to write cout rather than the full name which is std::cout. It's a convenience, that's all.
cout is defined in the std namespace, and you can use it without adding a using namespace like
std::cout << "Hello, World" << std::endl;
Thanks for the reply. But my question is why do we need it in the first place.
Since once we exposed "iostream" then why can't we simply use cout.
Why to use std::cout or using namespace std?
When you don't use the std namespace, compiler will try to call cout or cin as if it weren't defined in a namespace. Since it doesn't exist there, compiler tries to call something that doesn't exist! Hence, an error occurs.
I'm not new to programming but I am new to C++ using Xcode as the IDE. I am not allowed to use cout << "hello"; to print, the compiler complains and only works when I use std:cout <<. What setting in Xcode should be fixed in order to get this working the way I'm supposed to write it for class? Thanks!
Add using std::cout; somewhere close to the start of your program, after the includes.
Please avoid the temptation to use using namespace std;. It's really a bad habit. Just because you see other people doing it, doesn't make it right. See the FAQ: Why is "using namespace std" considered bad practice?
(Or, of course, you could just get use to typing std::cout.)
I had the same problem but I fixed it. You can simply write the following after the #include:
using namespace std;
I get these error messages for all cout and endl:
main.cc:17:5: error: ‘cout’ was not declared in this scope
main.cc:17:5: note: suggested alternative:
/usr/include/c++/4.6/iostream:62:18: note: ‘std::cout’
After following the suggestion, everything is fine. Now I am curious, why I had to do that. We used C++ in classes before, but I never had to write a std:: before any of those commands. What might be different on this system?
It seems possible your class may have been using pre-standard C++. An easy way to tell, is to look at your old programs and check, do you see:
#include <iostream.h>
or
#include <iostream>
The former is pre-standard, and you'll be able to just say cout as opposed to std::cout without anything additional. You can get the same behavior in standard C++ by adding
using std::cout;
or
using namespace std;
Just one idea, anyway.
In the C++ standard, cout is defined in the std namespace, so you need to either say std::cout or put
using namespace std;
in your code in order to get at it.
However, this was not always the case, and in the past cout was just in the global namespace (or, later on, in both global and std). I would therefore conclude that your classes used an older C++ compiler.
Everything in the Standard Template/Iostream Library resides in namespace std. You've probably used:
using namespace std;
In your classes, and that's why it worked.
You could use the namespace
http://www.daniweb.com/software-development/cpp/threads/109029/what-its-the-use-of-using-namespace-std
But you might offend someone
Why is "using namespace std" considered bad practice?
You probably had using namespace std; before in your code you did in class. That explicitly tells the precompiler to look for the symbols in std, which means you don't need to std::. Though it is good practice to std::cout instead of cout so you explicitly invoke std::cout every time. That way if you are using another library that redefines cout, you still have the std::cout behavior instead of some other custom behavior.
"std" is a namespace used for STL (Standard Template Library). Please refer to https://en.wikipedia.org/wiki/Namespace#Use_in_common_languages
You can either write using namespace std; before using any stl functions, variables or just insert std:: before them.
If you are working in ROOT, you do not even have to write #include<iostream> and using namespace std; simply start from int filename().
This will solve the issue.