命名空间的前置声明

9
namespace CounterNameSpace {
    int upperbound;
    int lowerbound;
    using namespace NS;//Error
}
namespace NS {
int i;
}
// ...
namespace NS {
int j;
}

在上述情况下,它显示了一个错误。 错误C2871:'NS':不存在此名称的命名空间。 我知道如果在counternamespace之前定义了NS,则问题将得到解决。但是,我想知道是否存在类似于C ++中的命名空间的前向声明的东西。这样可以在不在counternamespace之前定义NS的情况下解决上述问题。 请帮忙。

@user1872084:上面的链接与类的前向声明有关。 - Viku
1个回答

14

没有什么是必须立即使用命名空间中所有内容的:

namespace NS {}
namespace CounterNameSpace {
    int upperbound;
    int lowerbound;
    using namespace NS;
}
namespace NS {
int i;
}

然而,这可能不会达到您想要的效果。在您声明它们之前,仍然不能使用该命名空间中的任何类型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接