I encountered some issues while working on friend functions. I want to use a friend function that uses two different classes in parameters. Here is the sample of code:
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_ */
Both .cpp files for ObjectA and ObjectB are empty (empty constructor and destructor). Here is the main .cpp file:
#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;
}
This all thing sends me the following error :
'ObjectA' has not been declared
And this error is pointing to this line in the ObjectB.h :
friend void friendFunction(ObjectA &, ObjectB &);
As you can see, the ObjectA.h file has been included in the ObjectB.h file. So I don't know where my error come from.
Maybe I'm using friend function in a wrong way ?
Thank you guys !