![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 2
| Simple problem with classes 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
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
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 |
| desdelaspalmas is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| 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. |
| matsp is offline | |
| | #3 | |
| The larch Join Date: May 2006
Posts: 3,082
| 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. Quote:
| |
| anon is offline | |
| | #4 |
| Registered User Join Date: Nov 2008
Posts: 2
| Thank you !!!! I see it! |
| desdelaspalmas is offline | |
![]() |
| Tags |
| c++, class, extern, references |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem with classes | zdzislavv | C++ Programming | 20 | 12-07-2008 02:36 PM |
| Problem with simple XOR program | spike_ | C++ Programming | 8 | 08-17-2005 12:09 AM |
| problem with A simple modeless messagebox | hanhao | C++ Programming | 8 | 07-05-2005 11:18 PM |
| C / OpenGL help REALLY needed for simple but annoying problem! | Oz_joker | C Programming | 5 | 12-03-2003 05:47 PM |
| Simple class problem | savageag | C++ Programming | 1 | 10-04-2003 10:50 AM |