我在處理好友函數時遇到了一些問題。我想使用在參數中使用兩個不同類的朋友函數。下面是代碼的示例:在參數中需要2個類的朋友函數 - 未定義'類'
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_ */
爲對象A和對象B兩者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文件中。所以我不知道我的錯誤來自哪裏。
也許我用錯誤的方式使用朋友功能?
謝謝你們!
你是最棒的男人! – Xema 2014-11-02 17:00:15