gpt4 book ai didi

c++ - 引用表亲命名空间

转载 作者:行者123 更新时间:2023-11-28 00:57:46 25 4
gpt4 key购买 nike

我遇到了类似于 this question 的问题,但有一个额外的深度:

namespace root { namespace parent1 { namespace childa {
class hard_to_get_at{};
}}}

namespace root { namespace parent2 { namespace childb {
// how do I refer refer to namespace childb relative to the current namespace ?
void someFunc()
{
parent1::childa::hard_to_get_at instance; // does not work
}
}}}

当我尝试上面的方法时,出现错误

错误:'root::parent2::childb::parent1::childa' 尚未声明

我不明白为什么这行不通,但我觉得它应该行得通。我真的不想在 someFunc 函数中放置 using 声明。

这发生在启用了 c++0x 选项的 g++ 4.5 中

最佳答案

您缺少一些左括号:

namespace root { namespace parent1 { namespace childa { // <--- here
class hard_to_get_at{};
}}}

namespace root { namespace parent2 { namespace childb { // <--- and here
// how do I refer refer to namespace childb relative to the current namespace ?
void someFunc()
{
parent1::childa::hard_to_get_at instance; // does not work
}
}}}

这是缩进很重要的原因之一。

关于c++ - 引用表亲命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10165657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com