1

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

在 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'"
    };
  }
}

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

4

2 回答 2

0

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

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

于 2016-12-18T08:06:59.753 回答
0

如果您想避免将大型头文件包含到其他文件中,则至少需要在使用它们之前先声明您的类:

namespace A
{
  class Foo;

  namespace B
  {
    class Foo
    {
      friend class A::Foo;
    }
  }
}
于 2016-12-18T08:09:49.157 回答