Results 1 to 7 of 7

Thread: friend classes?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved friend classes?

    How in the hell can you use two classes inside each other in the
    same file?
    I could have sworn this used to work:
    Code:
    class cl1
    {
    	public:
    	int i;
    	cl1(cl2 c2){i = c2.i;}
    	friend class cl2;
    };
    
    // error: missing ')' before identifier c2
    
    class cl2
    {
    	public:
    	int i;
    	cl2(cl1 c1){i = c1.i;}
    	friend class cl1;
    };
    
    // this is fine
    Last edited by wey97; Nov 10th, 2004 at 09:59 AM.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    How about putting:
    Code:
    class cl2;
    ...above the two definitions?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    error: use of undefined type 'cl2'
    Code:
    class cl2;
    
    class cl1
    {
    	public:
    	int i;
    	cl1(cl2 c2){i = c2.i;}
    	friend class cl2;
    };
    
    class cl2
    {
    	public:
    	int i;
    	cl2(cl1 c1){i = c1.i;}
    	friend class cl1;
    };

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    I guess it could be a circular definition type thing.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    don't type the definitions inline
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Member
    Join Date
    Jun 2003
    Posts
    43

    Smile

    Hi ,

    U need forward declaration of the class cl2.

    Just write "class cl2" before class cl1 declaration.
    Try it..

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Wrong avaneesh, that won't suffice.

    What keda said. Put the implementations of cl1's methods after the definition of cl2.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width