开发者

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(s开发者_C百科td::map<std::string,Widget *>::iterator w = widgets_.begin();


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜