Scott
Scott

Reputation: 5263

using namespace issue

When I use the following

#include <map>

using namespace LCDControl;

Any reference to the std namespace ends up being associated with the LCDControl name space.

For instance:

Generic.h:249: error: 'map' is not a member of 'LCDControl::std'

How do I get around this? I didn't see anything specific to this on any documentation I looked over. Most of them said not to use: using namespace std;.

Here's line 249:

for(std::map<std::string,Widget *>::iterator w = widgets_.begin();

Upvotes: 0

Views: 285

Answers (2)

Tim Sylvester
Tim Sylvester

Reputation: 23138

It looks like there's a std namespace within LCDControl that's hiding the global std namespace. Try using ::std::map instead of std::map.

I would say that either there's a using namespace std somewhere within the LCDControl namespace, or possibly there's an #include of a STL header that defines std within the LCDControl namespace.

e.g.:

namespace LCDControl
{
    #include <map>
}

Which would define all the symbols in <map> as part of LCDControl::std, which in turn would hide the global std, or at least any symbols defined in the inner namespace, I'm not sure.

When I tried this under VS2008, I got an error:

namespace testns
{
    int x = 1;
}

namespace hider
{
    namespace testns
    {
        int x = 2;
    }
}

int y = testns::x;
using namespace hider;
int z = testns::x;    // <= error C2872: 'testns' : ambiguous symbol

Upvotes: 4

Ed Swangren
Ed Swangren

Reputation: 124652

The 'map' class lives in the std namespace, so you are going to have to qualify that somewhere. How are you qualifying your map object? You should have no problem doing this:

std::map<foo> myMap;

You can also do something like this if you do not want to explicitly qualify it every time, but also do not want to pollute your global namespace:

using std::map;

Upvotes: 1

Related Questions