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 .

When you forward declare a class, you only tell the compiler that it exists, not what's in it. So while the declarations are fine, the definition of FileTwo::Test() would need to be deferred until after both classes have been introduced completely:

class FileOne;

class FileTwo
{
public:
    int Test(FileOne One);
};

class FileTwo;

class FileOne
{
private:
    int var1, var2, var3;
public:
    friend int FileTwo::Test(FileOne One);

    FileOne()
    {
        var1 = 12;
        var2 = 24;
    }
};

// Now this definition will work
int FileTwo::Test(FileOne One)
{
    return (One.var1 + One.var2);
}

#include <iostream>

using namespace std;

int main()
{
    FileOne one;
    FileTwo two;

    cout << two.Test(one) << '\n';
}

To facilitate this with multiple files and headers, I'd suggest separating your class declarations and definitions into a header and implementation file. First the header:

// FileTwo.h
#ifndef FILETWO_H
#define FILETWO_H

class FileOne;

class FileTwo
{
public:
    int Test(FileOne One);
};

#endif

Then the implementation file which handles definitions:

// FileTwo.cpp
#include "FileOne.h"
#include "FileTwo.h"

int FileTwo::Test(FileOne One)
{
    return (One.var1 + One.var2);
}

I already posted in my post that i know this solution of implementing it in one cpp file . But just for the learning purpose i want to know whether any solution for this is there inside header file except making FileTwo class itself as friend .

Thanks deceptikon for replying ..Your post always helps me a lot ..

I already posted in my post that i know this solution of implementing it in one cpp file

Header files are a glorified cut and paste mechanism. If you're having trouble with your headers, then manually creating the same effect in a single file can help highlight the problem. You said you fixed it by using a single file, but clearly you made some sort of change that differs from how your compiler would have done it, otherwise the error would persist even using a single file. Here's how the translation unit would look with your headers directly included:

#include<iostream>
using namespace std ;
class FileOne ;
class FileTwo
{
public:
    int Test(FileOne One){
        return (One.var1+One.var2);}
    FileTwo(void);
    ~FileTwo(void);
};
class FileTwo ;
class FileOne
{
private:
    int var1 , var2 , var3 ;
public :
    friend int FileTwo::Test(FileOne One);
    FileOne(){
        var1= 12;var2 = 24;
    }
};
int main(){
    FileOne one ;
    FileTwo two ;
    cout<<two.Test(one);
}

The error is still present, and it's much easier to see now that everything is all together in one file. Once you fix the error in a single file, you can break it back up into multiple files. And in this case, the fix is to separate the member function declarations from their corresponding definitions (ie. use a header/implementation file setup).

ya thanks . I got it after pasted all into one file .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.