Dev C++ using namespace.std issues

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?
Can you provide a sample snippet?
Ok here goes. This is the code I tried to compile

#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.

Technically, you shouldn't be able to compile it, regardless of whether or not the offending line is inside a function or not.

using namespace.std; makes no sense.

using namespace std; is the proper way.
Ahh...
Best not to do it at all, put std:: before each std thing, as in std::cout. There is lots written as to why this is so.

I don't mean to step on xismn's toes (I am sure he knows much more than me), it is just that this bad habit is one of my pet peeves :+D
Last edited on
You can also use using std::cout; as another alternative this will give you access to the output stream without all the std:: prefixes.

@TheIdeasman - Is this an acceptable method?
Last edited on
@CodeGoggles

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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

namespace bg = boost::geometry;

int main()
{
    bg::model::box<bg::model::d2::point_xy<double> > box;

    bg::assign_values(box, 1, 3, 5, 6);

    std::cout << "Box:"
        << " " << bg::get<bg::min_corner, 0>(box)
        << " " << bg::get<bg::min_corner, 1>(box)
        << " " << bg::get<bg::max_corner, 0>(box)
        << " " << bg::get<bg::max_corner, 1>(box)
        << std::endl;

    return 0;
}


Btw, one should put their own code in it's own namespace.
@TheIdeasman

I've noticed in some code I've browsed, blank namespaces. for example.

1
2
3
4
5
namespace
{
	// GLOBAL POINTER TO CLASS INSTANCE FOR HANDLING OF WINPROC
	DXApp *gp_class = nullptr;
}


My question is why would you do this?

Edit: I'm not referring to the code in the namespace just why the blank namespace?
As it is code I wrote myself from an example.
Last edited on
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.

http://stackoverflow.com/questions/3363684/anonymous-namespace
Okay cheers, I figured that was the reason just needed clarification.
Topic archived. No new replies allowed.