GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Oct-2009, 05:13
katty katty is offline
New Member
 
Join Date: Oct 2009
Posts: 2
katty is on a distinguished road
Thumbs up

Friend function


Hi folks,,

I've tried to write below program to understand 'friend' functions

CPP / C++ / C Code:
#include <iostream>
using namespace std;

class C2;

class C1
{
	int value;

public:
       friend void C2::get_other(C1 a);
       void set(int i);
};


class C2
{
	int value;

public:
        void get_other(C1 a);
        void set(int i);
};


void C2::get_other(C1 a)
{
	cout<<"C2 val="<<value<<endl;
	cout<<"c1 val= "<<a.value<<endl;
}


void C1::set(int i)
{
	value = i;
}


void C2::set(int i)
{
	value = i;
}



int main()
{
	C1 a;
	C2 b;
	a.set(4);
	b.set(5);
	b.get_other(a);
	return 0;
}


And I got following compilation error.. plz give suggestions to solve these

name.cc:8: error: invalid use of incomplete type ‘struct C2’
name.cc:3: error: forward declaration of ‘struct C2’
name.cc: In member function ‘void C2::get_other(C1)’:
name.cc:6: error: ‘int C1::value’ is private
name.cc:23: error: within this context
  #2  
Old 26-Oct-2009, 06:21
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: friend function


Try reading:

http://msdn.microsoft.com/en-us/libr...he(VS.80).aspx

...then take a look at:

http://publib.boulder.ibm.com/infoce...ef/cplr042.htm


MxB
  #3  
Old 27-Oct-2009, 09:23
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Friend function


Well I'll be dog, I think I'm understanding this better.
What's wrong with this M?
CPP / C++ / C Code:
#include <iostream>
using namespace std;

class C2;

class C1
{
  int value;

  public:
    // friend void C2::get_other(C1 a);    No, either make C2 a friend
    friend class C2;

    // or make separate function a friend
    friend void funcky1(C1);

    void set(int i) { value = i; }
};


class C2
{
  int value;

  public:
    void set(int i) { value = i; }
    void get_other(C1 a)
    {
      cout<<"C2 val="<<value<<endl;
      cout<<"c1 val= "<<a.value<<endl;
    }
};

void funcky1(C1 a)     // separate function
{
  cout<<"C1 type a.value= "<<a.value<<endl;
}

int main()
{
  C1 a;
  C2 b;
  a.set(4);
  b.set(5);
  b.get_other(a);
  funcky1(a); 
  return 0;
}
I use this as a reference as I have dialup and msdn is a .NET black hole.
cplusplus.com/doc/tutorial/inheritance.html
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 66 16-Jan-2010 10:53
Problem executing nam-1.13 RodolfoAlvizu Computer Software Forum - Linux 20 28-Feb-2009 15:23
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 11:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 16:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:59.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.