在嵌套命名空间的不同级别中具有相同名称的 C++ 友元类

标签 c++ namespaces friend friend-class

不确定这是否可能,但我在嵌套命名空间的不同级别中有两个同名的类,我想让更浅的类成为更深的类的 friend 。示例:

在 File1.h 中:

namespace A
{
  class Foo
  {
    //stuff
  };
}

在 File2.h 中:

namespace A
{
  namespace B
  {
    class Foo
    {
      friend class A::Foo; //Visual Studio says "Error: 'Foo' is not a member of 'A'"
    };
  }
}

这可能吗?如果是这样,正确的语法是什么?

最佳答案

此代码放在一个文件中时编译正常(除了 ;A::B::Foo 类之后是必需的):IdeOne example .

因此,问题出在问题文本中未包含的代码中。可能 #include "File1.h" 被遗忘在 File2.h 中。

关于在嵌套命名空间的不同级别中具有相同名称的 C++ 友元类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41206387/

相关文章:

c++ - Xlib。检查窗口是否重叠

Java XPath : Get all the elements that match a query

c# - C# 中是否有名称为 "LinqToVisualTree"的命名空间?

c++ - 定义 ostream 运算符的友元函数

java - Java中类C++的友元类机制

c++ - 多重继承,没有函数重写。为什么有歧义

c++ - 逻辑错误,需要协助

c++ - std::decay 零长度数组

php - Laravel 5 数据库播种中的命名冲突

c++ - 模板类的模板友元函数