班级交友
我试图与一个类成为朋友,以便它能够到达它的私有构造函数。
在 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)”是私有的。
我现在,这是私人的,这就是我和B成为朋友的原因。 我在这里做错了什么? 你不能和你的构造函数交朋友吗? 是否存在命名空间问题?
谢谢
I am trying to befriend a class in order for it to be able to reach a private constructor of it.
In some_file.h
class B;
namespace some_name {
class A {
public:
A() {}
private:
A (int x) {}
friend class ::B;
};
}
In other_file.h
#include "some_file"
namespace {
class B {
protected:
A* get_a(int x) { return new A(x); }
};
}
When compiling this code, I get -
error: 'some_name::A::A(int)' is private.
I now, it is private, this is why I befriended B.
What am I doing wrong here?
Can't you befriend your constructor?
Is there a namespace issue?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这样做:
您不会将
B
放入根(全局)命名空间中,而是放入匿名命名空间中。因此
::B
无法访问B
。如果您希望
B
位于根(全局)命名空间中,则根本不要将其包含在namespace
中。这应该可以解决问题。Doing this:
You're not putting
B
in the root (global) namespace but in an anonymous one.So
B
can't be reached by::B
.If you want
B
to be in the root (global) namespace, just don't enclose it withnamespace
at all. This should do the trick.您仅在全局命名空间中转发声明并添加了类 B。无论如何,不是命名空间中的 B 类。您需要完全限定 B 的名称。
编辑:谢谢 ereOn。
我犯了一个小错误。确实,你遇到问题的原因是因为你错误地声明并错误地引用了B,但我最初的陈述并不完全正确。您需要将 B 从匿名名称空间中取出 - 无论如何,它位于标头中是没有意义的。
You only forward declared and friended a class B in the global namespace. Not a class B in a namespace whatever. You need to fully qualify the name of B.
Edit: Thanks ereOn.
I made a slight mistake. It's true that the reason that you've got a problem is because you've mis-declared and mis-referred-to B, but my original statement wasn't quite true. You need to take B out of the anonymous namespace - it's pointless being in a header anyway.
问题是您将
B
引用为::B
而不是B
。意思是,您告诉编译器B
是全局名称,但实际上它不是:它位于匿名命名空间内。您不必删除匿名名称空间,只是它可能不会执行您期望的操作。因为匿名命名空间位于标头中,所以意味着该命名空间内的内容静态链接到包含该标头的任何实现文件。这不是很有用,因为你没有隐藏任何东西。您不妨删除该匿名名称空间。The problem is that you refer to
B
as::B
instead ofB
. Meaning, you're telling the compiler thatB
is a global name, but in fact it isn't: it's inside an anonymous namespace. You don't have to remove the anonymous namespace, it's just that it may not do what you expect it to do. Because the anonymous namespace is in a header it means what's inside that namespace is linked statically to any implementation file that includes the header. That's not very useful, because you hide nothing. You might as well remove that anonymous namespace.可以如下图所示
You can as shown below