Mutual linking

This is a discussion on Mutual linking within the C++ Programming forums, part of the General Programming Boards category; Hi everybody! I have a short question about mutual linking (this is what I call it, I don't know if ...

  1. #1
    Mox
    Mox is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    41

    Unhappy Mutual linking

    Hi everybody!

    I have a short question about mutual linking (this is what I call it, I don't know if it's the right name...)

    I have 2 classes and they both have members of the type of the other class:

    //header.h

    class cls_one
    {
    cls_two * member;
    };

    class cls_two
    {
    cls_one * member;
    };

    obviously this doesn't compile, but this is the way I want to do it. Is there a way to do this?

    thanks

    Joren

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You need to put a forward declaration in your code so that cls_one knows about cls_two.

    Code:
    //header.h 
    class cls_two;
    
    class cls_one 
    { 
    	cls_two * member; 
    }; 
    
    class cls_two 
    { 
    	cls_one * member; 
    };

  3. #3
    Mox
    Mox is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    41
    Thanks! That worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems linking with g++
    By Just in forum Linux Programming
    Replies: 11
    Last Post: 07-24-2006, 02:35 AM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 11:51 PM
  3. Replies: 8
    Last Post: 04-27-2006, 11:39 AM
  4. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 09:44 PM
  5. Linking error with DirectDrawCreateEx
    By jjj93421 in forum Game Programming
    Replies: 6
    Last Post: 04-06-2004, 04:57 PM

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