gpt4 book ai didi

c++ - '使用命名空间{某些命名空间嵌套在另一个内部}`

转载 作者:行者123 更新时间:2023-12-01 14:04:19 24 4
gpt4 key购买 nike

是否可以访问类型Foo命名空间下 first通过在调用者代码中使用 using 声明(或类似的东西)?

namespace first {
namespace second {
struct Foo { int i; };
}
}

int main() {
using namespace first::second;
first::Foo foo { 123 };
return 0;
}
我收到这些错误消息:
error: 'Foo' is not a member of 'first'
first::Foo foo{ 123 };```

最佳答案

您有多种选择:

  • using namespace first::second; 
    Foo foo{123};
  • namespace ns = first::second; 
    ns::Foo foo{123};
  • 我想你也可以做 namespace first = first::second; .然后first::Foo foo{123};会工作,但要访问实际 namespace first 的内容(除了 namespace second 中的那些)你必须使用 ::first .
  • 关于c++ - '使用命名空间{某些命名空间嵌套在另一个内部}`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62745184/

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