Thread: Simple problem with classes

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Simple problem with classes

    Hi everyone,


    Well, I have 3 classes in 3 files:


    Main class father, at father.h:
    Code:
    #if ! defined ( Father_C )
    #define Father_C
    
    #include <stdio.h>
    
    #include "son.h"
    #include "daughter.h"
    
    
    class Father
    {
    private:
    	son *sons;
            daughter *daughters;
    
    public:
    	Father(){
    		son jack;
    		
    	};
    	~Father();
    
    };
    
    #endif
    at son.h:
    Code:
    #if ! defined (Son_C )
    #define Son_C
    
    #include <stdio.h>
    
    #include "daughter.h"
    
    
    class son
    {
    private:
            daughter *sisters;
    
    public:
    	son(){
    		daughter jamie;
    		
    	};
    	~son();
    
    };
    
    #endif
    at daughter.h:
    Code:
    #if ! defined (Daughter_C )
    #define Daughter_C
    
    #include <stdio.h>
    
    #include "son.h"
    
    
    class daughter
    {
    private:
           son *brothers;
    
    public:
    	daughter(){
    		son jack;
    		
    	};
    	~daughter();
    
    };
    
    #endif

    But it doesnt work, I am unable to reference 2 classes between themselves, as I did in the example with son and daughter. I would like to references those clases between them and with the father, as i did at the example.

    I know it is not much difficult, I will appreciate a lot any help or references.


    Thank you very much

    David

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What you are trying to do is a circular arrangement of classes, where class A contains class B, which in turn contains class A. Since the compiler needs to know the size and content of A to know the size and content of B, you'll end up with an infinite loop of unknown sizes.

    You can use a reference or a pointer, but not include the class itself inside another class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have to use forward declarations instead of including the other headers. To declare a pointer (or a reference) the full type doesn't have to be known, the compiler simply needs to be assured that a class by this name exists.

    Also see the comment about the constructor:

    Code:
    #if ! defined ( Father_C )
    #define Father_C
    
    //#include <stdio.h>  //not used
    
    //#include "son.h"     //don't have to be included 
    //#include "daughter.h" //and cannot be if you have circular dependencies
    
    //tell compiler that classes called son and daughter exist
    class son;
    class daughter;
    
    class Father
    {
    private:
    	son *sons;
            daughter *daughters;
    
    public:
    	Father(){
    		//son jack;
    /*
    This doesn't do anything - it creates a local son object that only exists in the constructor
    If you really need to do something like that, the full definition of Son must be available:
    you have to provide the implementation in the separate cpp file that can include "son.h"
    */
    		
    	};
    	~Father();
    
    };
    
    #endif
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Thank you !!!! I see it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes
    By zdzislavv in forum C++ Programming
    Replies: 20
    Last Post: 12-07-2008, 02:36 PM
  2. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  3. problem with A simple modeless messagebox
    By hanhao in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2005, 11:18 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Simple class problem
    By savageag in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2003, 10:50 AM

Tags for this Thread