Like Tree4Likes
  • 2 Post By Salem
  • 2 Post By Salem

confused with friend function

This is a discussion on confused with friend function within the C++ Programming forums, part of the General Programming Boards category; Inside FileTwo.h Code: #ifndef FILETWO #define FILETWO #include "FileOne.h" class FileOne ; class FileTwo { public : int Test ( ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    Lesshardtofind and Elysia like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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
    vikuseth and Elysia like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08: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, 09:53 PM
  5. Friend Function
    By s0ul2squeeze in forum C++ Programming
    Replies: 1
    Last Post: 04-03-2002, 01:50 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21