Mutual Classes

This is a discussion on Mutual Classes within the C++ Programming forums, part of the General Programming Boards category; Hi I have the following problem. I have two classes and two header files. However, One class needs to use ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Mutual Classes

    Hi I have the following problem. I have two classes and two header files. However, One class needs to use the other, and vice-versa. Now when I compile, because a has already been defined, when the preprocessor comes to class b, it will open up class a's header file find that a has already been defined (because the preprocessor did class a first), and therefore a's class definition will not get inserted into class b's header file. Then the compiler will throw an error will say a is undefined within class b. Which is correct. How do I get around this problem? If I got rid of the preprocessor commands I would probably get a class redefinition error... Code below is only illustration!

    Code:
    #ifndef a
    #define a
    
    #include "b.h"
    
    Class a
    {
      b *tmp;
    }
    #endif
    
    #ifndef b
    #define b
    
    #include "a.h"
    
    Class b
    {
      a *tmp;
    }
    #endif
    Any help would be appreciated. Additionally, if some of my thinking is slightly off regarding the actions of the preprocessor, please dont have a go.
    Be a leader and not a follower.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    class a;
    class b;
    
    class a {
      b* ptr;
    }
    
    class b {
      a* ptr;
    }
    btw its class not Class

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    So I wouldn't be able to seperate the two classes into two different files? Would both classes have to be declared within the same .h file?
    Be a leader and not a follower.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you can easily seperate them into different files. You just have to let the compiler know that there is a class called a and a class called b prior to using them.

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    So say in the header file for class a, what preprocessor command would I need to use so that the compiler knows of class b's existance, and that class a is going to be using class b?
    Be a leader and not a follower.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The easist way is to have a third header file that just declares classes without defining them.

    header 1
    Code:
    #ifndef T1_H_
    #define T1_H_
    
    #include "tall.h"
    
    class a {
      b *ptr;
    };
    
    #endif
    header 2
    Code:
    #ifndef T2_H_
    #define T2_H_
    
    #include "tall.h"
    
    class b {
      a *ptr;
    };
    
    #endif
    header 3 (tall.h)
    Code:
    #ifndef TALL_H_
    #define TALL_H_
    
    class a;
    class b;
    
    #endif

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Thats exactly what I needed, Thank you.
    Be a leader and not a follower.

  8. #8
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Hi, I tried using your first example, but got compiler errors, so I included the class defintion and implementation directly into my main cpp file. Though this time it says : "'b::test' uses undefined class 'a'"

    Code:
    #include <tchar.h>
    
    class a;
    class b;
    
    class b
    {
    	a test;
    	b(){};
    };
    
    class a
    {
    	b test;
    	a(){};
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	return 0;
    }
    Any ideas on how to get this working?
    Be a leader and not a follower.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    ok now its a problem. The first time you had pointers to the other class. That is fine. Now you want an object of that other class. That is not possible. If it was allowed what you would end up with is a recursive defination between the two that would continue on to infinatity.

  10. #10
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Your a legend cocker, got it working within my main project now instead of that test console one. Thanks for your help again. Dene
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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