C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-27-2008, 05:43 AM   #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
desdelaspalmas is offline   Reply With Quote
Old 11-27-2008, 06:09 AM   #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   Reply With Quote
Old 11-27-2008, 06:36 AM   #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:
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).
anon is offline   Reply With Quote
Old 12-01-2008, 03:15 AM   #4
Registered User
 
Join Date: Nov 2008
Posts: 2
Thank you !!!! I see it!
desdelaspalmas is offline   Reply With Quote
Reply

Tags
c++, class, extern, references

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:56 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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