使用命名空间问题

发布于 2024-08-08 17:51:57 字数 440 浏览 2 评论 0原文

当我使用以下内容时,

#include <map>

using namespace LCDControl;

对 std 命名空间的任何引用最终都会与 LCDControl 命名空间相关联。

例如:

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

我该如何解决这个问题?我在查看的任何文档中都没有看到任何与此相关的内容。大多数人都说不要使用:using namespace std;。

这是第 249 行:

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

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2024-08-15 17:51:57

看起来std lcdcontrol中隐藏了全局std namespace。尝试使用:: STD :: MAP而不是std :: Map

我要说的是,要么使用使用namespace stdlcdcontrol命名空间中的某个地方,或者可能有一个#include的定义来定义 代码> std 在lcdcontrol名称空间中。

例如:

namespace LCDControl
{
    #include <map>
}

它将在&lt; map&gt;中定义所有符号,作为lcdcontrol :: std的一部分,这又将隐藏全局std ,或者至少在内部名称空间中定义的任何符号,我不确定。

当我在VS2008下尝试此操作时,我遇到了一个错误:

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

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
音盲 2024-08-15 17:51:57

“map”类位于 std 命名空间中,因此您必须在某个地方对其进行限定。您如何限定您的地图对象?这样做应该没有问题:

std::map<foo> myMap;

如果您不想每次都显式限定它,但也不想污染您的全局名称空间,您也可以这样做:

using std::map;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文