Inside FileTwo.h
#ifndef FILETWO
#define FILETWO
#include"FileOne.h"
class FileOne ;
class FileTwo
{
public:
int Test(FileOne One){
return (One.var1+One.var2);}
FileTwo(void);
~FileTwo(void);
};
#endif
Inside FileOne.h
#ifndef FILEONE
#define FILEONE
#include"FileTwo.h"
class FileTwo ;
class FileOne
{
private:
int var1 , var2 , var3 ;
public :
friend int FileTwo::Test(FileOne One);
FileOne(){
var1= 12;var2 = 24;
}
};
#endif
Inside main.cpp
#include<iostream>
using namespace std ;
#include"FileOne.h"
#include"FileTwo.h"
int main(){
FileOne one ;
FileTo two ;
cout<<two.Test(one);
}
During compilation i got the following error
1-- error C2027: use of undefined type 'FileOne' c:\users\e543925\documents\visual studio 2005\projects\myproject\filetwo.h
2--error C2027: use of undefined type 'FileOne' c:\users\e543925\documents\visual studio 2005\projects\myproject\filetwo.h
3--error C2228: left of '.var1' must have class/struct/union c:\users\e543925\documents\visual studio 2005\projects\myproject\filetwo.h
4--error C2228: left of '.var2' must have class/struct/union c:\users\e543925\documents\visual studio 2005\projects\myproject\filetwo.h
I have found one workaround like definning the Test function inside FileTwo.cpp . But i want to know how the above issue can be resolved inside header file .
Your problem here is that you are including both files in each other. When your compiler goes to parse FileOne.h
, it includes FileTwo.h
, so it reads that, skips FileOne.h
(thanks to the include guards; otherwise, you would go into an infinite loop) and tries to use class FileOne
, which is not yet defined.
In practice, this is just as if you had not included FileOne.h
in FileTwo.h
; you cannot call methods on One
because its type (FileOne
) is not yet defined. Nothing to do with friend classes. Or not yet (you will get into this problem later).
From your code, it looks like you want to use class FileTwo
to test FileOne
. In this case, FileOne
does not really need to know much about FileTwo
, just allow it to look at its innards (aka make it a friend). So your code can boil down to:
FileOne.h:
#ifndef FILEONE
#define FILEONE
class FileOne
{
friend class FileTwo; // This is all FileOne needs from FileTwo
private:
int var1 , var2 , var3 ;
public :
FileOne(){
var1= 12;var2 = 24;
}
};
#endif
FileTwo.h:
#ifndef FILETWO
#define FILETWO
#include"FileOne.h"
class FileTwo
{
public:
int Test(FileOne One) {
return (One.var1+One.var2);
}
FileTwo();
~FileTwo();
};
#endif