友元声明阻止外部函数访问类的私有部分
friend declaration block an external function access to the private section of a class
我试图强制函数调用程序来自特定的类。例如,下面的代码演示了我的问题。我想让"use"函数只能从类A中调用。我在整个项目中使用全局命名空间。
a.h
#include "b.h"
namespace GLOBAL{
class A{
public:
void doSomething(B);
}
}
a.cp
#include "a.h"
using namespace GLOBAL;
void A::doSomething(B b){
b.use();
}
b.h
namespace GLOBAL{
class B{
public:
friend void GLOBAL::A::doSomething(B);
private:
void use();
}
编译器说:
‘GLOBAL::A’ has not been declared
‘void GLOBAL::B::use()’ is private
这里有人能帮忙吗?
非常感谢
迈克。
这是因为在朋友delcaration中,您引用的是一个类的成员。
为了使其工作,编译器必须已经看到A.的完整定义
a.h
// #include "b.h" // remove this line it is not needed.
namespace GLOBAL{
class B; // Forward declare the class here.
class A{
public:
void doSomething(B&); // Note: This should probably be a reference.
// Change to doSomething(B&);
}
}
b.h
// Add this line it is needed for the friend declaration.
// To have a member as a friend we need the definition of 'A'
#include "a.h"
namespace GLOBAL{
class B{
public:
friend void GLOBAL::A::doSomething(B&);
private:
void use();
}
a.cpp
#include "a.h"
// Add this line to include the B definition so you know how to call use()
#include "b.h"
using namespace GLOBAL;
void A::doSomething(B& b){ // b should be a reference otherwise you are copying it.
b.use();
}
以下从cpp文件中编译良好:
namespace GLOBAL
{
class B;
class A
{
public:
void doSomething(B& b);
};
};
namespace GLOBAL
{
class B
{
public:
friend void GLOBAL::A::doSomething(B&);
private:
void use()
{
}
};
};
void GLOBAL::A::doSomething(B& b)
{
b.use();
}
我可以告诉你,你的问题源于这样一个事实,即在定义a类之前,你包含了定义b类的"a.h"中的"b.h",而b类引用了a类。所以你有问题。但是,您不能正向声明B类型的对象,因为您是通过堆栈进行复制的。因此,我使用对类B的引用(因为这不需要事先知道B对象)。
基本上,你的结构有一些基本问题需要解决。你需要做一些关于前向声明和循环依赖的阅读
编辑:纯粹指定类A是B的友元(而不是A中引用B的特定函数)实际上是一种可能性,因为友元定义提供了一种前向声明。因此,以下代码编译:
namespace GLOBAL
{
class B
{
public:
friend class A;
private:
void use()
{
}
};
};
namespace GLOBAL
{
class A
{
public:
void doSomething(B b);
};
};
void GLOBAL::A::doSomething(B b)
{
b.use();
}
因此,在你的代码中,最初发布的,将朋友的声明更改为,简单的
friend class A;
应该允许您的代码进行编译。
在第一个#include
指令之后将#include "b.h"
从a.h
移动到a.cpp
。编译器不需要看到友元声明,直到它编译了声明所应用的函数
相关文章:
- C++:无法访问声明的受保护成员
- 访问在 C++ 结构中声明的枚举变量
- 模板方法访问正向声明的类仅在没有此指针的情况下无法编译
- 为什么即使我声明了朋友类,我也会收到错误"无法访问类中声明的私人成员"
- 在 C++ 中声明和访问 C 库中的结构
- 在使用受保护和继承时无法访问在类中声明的私有成员
- 如何声明指向成员内容的成员指针m_pmd/m_pmf并访问它们?
- 如何访问转发声明类的成员
- 在 if 语句中声明时如何访问类对象?(在 C++ 中)
- 在类访问混淆中声明的枚举类型
- 在 c++ 中直接访问的内联数组声明
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- 使用好友声明访问私有模板
- C++中成员名称查找和访问声明中的歧义
- 在派生的C 类中,访问基类受保护成员作为公共的访问声明
- 如何在 SWIG 中访问声明的模板结构变量
- xcode 5.1.1抱怨:ISO c++ 11不允许访问声明
- 不赞成使用访问声明,而是使用-declarations;建议:添加“using”关键字
- 公共访问声明不会影响成员函数指针