我正在尝试与一个类成为朋友,以便它能够访问它的私有构造函数。
在 some_file.h
class B;
namespace some_name {
class A {
public:
A() {}
private:
A (int x) {}
friend class ::B;
};
}
在 other_file.h
#include "some_file"
namespace {
class B {
protected:
A* get_a(int x) { return new A(x); }
};
}
编译此代码时,我得到 - 错误:'some_name::A::A(int)' is private。
我现在,它是私人的,这就是我与 B 成为朋友的原因。我在这里做错了什么?你不能和你的构造函数交朋友吗?有命名空间问题吗?
谢谢