Hi. I'm running myself through Bucky's C++ tutorials and have hit a bit of a snag. One of the tutorials explained that if I include using namespace.std right under the #include <whatever> statements it will automatically place namespace.std in every function in the program. But when I try to do it, I get a bunch of compiler errors unless I place it manually into each function, and I get errors if I try to put using namespace anywhere in the program besides a function. What am I doing wrong?
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace.std;
int main(){
int bacon [5] = {2,8,6,7,9};
cout << bacon [2];
cin.get();
return 0;
}
Like I said, when I include the using namespace inside the function no problems occur.
But when I try to use it outside of a function I get a bajillion errors.
Well it works & might be easier, I used to do it all the time. But if you look at the expert user's code on this site (& others) you will see they always just put std::.
One advantage is that if I send you a function full of std:: , then you don't have to do anything to it. But if I had a bunch using statements at the beginning of my file, then you would have to work out which ones to add to your file, or add std:: everywhere. So just have std:: in the first place.
Another handy thing is to make an alias with a using statement. This is useful when there are lots of nested namespaces, as in :
I've noticed in some code I've browsed, blank namespaces.
Those are called anonymous namespaces. You use an anonymous namespace when you don't wish the things contained therein to be accessible outside the current translation unit.