Thread: confused with friend function

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    confused with friend function

    Inside FileTwo.h

    Code:
    #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

    Code:
    #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

    Code:
    #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

    Code:
     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 .

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A good idea is to REVIEW your post before pressing submit, to make sure that what you hastily copy/pasted from elsewhere is actually READABLE.

    Yours is far and away an unreadable mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Case in point
    c++ - confused with friend function - Stack Overflow

    Which was only 20 freaking minutes ago.

    Here's some medicine for you.
    How To Ask Questions The Smart Way
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend Function
    By Suchy in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2006, 09:48 PM
  2. about template and friend function
    By f6ff in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2006, 01:48 AM
  3. Friend function
    By Verdagon in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 11:44 AM
  4. friend function and friend classes...usage question??
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 10:53 PM
  5. Friend Function
    By s0ul2squeeze in forum C++ Programming
    Replies: 1
    Last Post: 04-03-2002, 02:50 PM

Tags for this Thread