【发布时间】:2014-11-02 16:37:17
【问题描述】:
我在处理朋友功能时遇到了一些问题。我想使用一个在参数中使用两个不同类的朋友函数。这是代码示例:
ObjectA.h:
#ifndef OBJECTA_H_
#define OBJECTA_H_
#include "ObjectB.h"
#include <iostream>
using namespace std;
class ObjectA {
private:
friend void friendFunction(ObjectA &,ObjectB &);
public:
ObjectA();
virtual ~ObjectA();
};
#endif /* OBJECTA_H_ */
ObjectB.h:
#ifndef OBJECTB_H_
#define OBJECTB_H_
#include <iostream>
using namespace std;
#include "ObjectA.h"
class ObjectB {
private:
friend void friendFunction(ObjectA &, ObjectB &);
public:
ObjectB();
virtual ~ObjectB();
};
#endif /* OBJECTB_H_ */
ObjectA 和 ObjectB 的 .cpp 文件都是空的(空的构造函数和析构函数)。这是主要的 .cpp 文件:
#include <iostream>
using namespace std;
#include "ObjectA.h"
#include "ObjectB.h"
void friendFunction(ObjectA &objA, ObjectB &objB){
cout << "HIIIIIIIIIII";
}
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
这一切都向我发送了以下错误:
'ObjectA' has not been declared
这个错误指向 ObjectB.h 中的这一行:
friend void friendFunction(ObjectA &, ObjectB &);
如您所见,ObjectA.h 文件已包含在 ObjectB.h 文件中。所以我不知道我的错误来自哪里。
也许我以错误的方式使用朋友功能?
谢谢你们!
【问题讨论】:
标签: c++ friend-function