delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/04/12/01:37:23

From: "Niklas Pettersson" <npedt97 AT student DOT vxu DOT se (nospam)>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: classes?
Date: Wed, 12 Apr 2000 07:14:09 +0200
Organization: Lund Institute of Technology, Sweden
Lines: 58
Message-ID: <8d10nv$72m$1@news.lth.se>
References: <38F3FDB4 DOT DCDEBD61 AT rug DOT ac DOT be>
NNTP-Posting-Host: npedt97.univ.vxu.se
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2919.6600
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

It's because you try to include the files in cycle. For example you have a
Class 1 hat needs to know that Class 2 exists to be created but Class 2 need
to know class 1!!
(Which should be created first? the chicken or the egg)

The solution is to make a partial declaration of one class

Class Class2; // put in one of the files instead of the include

in that way the cycle is broken.

/ Niklas



"Bram de Greve" <bram DOT degreve AT rug DOT ac DOT be> wrote in message
news:38F3FDB4 DOT DCDEBD61 AT rug DOT ac DOT be...
> If have this strange problem, can anyone help me please ???
>
> Say, I have two classes Class1 and Class2.
> I have the the stuff (interface and implementation) of Class1 in
> class1.hpp and class1.cpp.
> The stuff of Class2 is in class2.hpp and class2.cpp.
> Both classX.hpp are done with
>    #ifndef __CLASSX_HPP_
>    #define __CLASSX_HPP_
>    ...
>    #endif
> so I can't include twice the same file.
> So far, so good,  I can compile both class1.cpp and class2.cpp.
>
> Now, in Class1, I need a member object of Class2
>    class Class1
>    {
>       Class2 * myClass2;
>       ...
>    };
> Thus in class1.hpp, I include "class2.hpp" so that Class1 "knows"
> Class2.
>
> I also need a member object of Class1 in Class2
>    class Class2
>    {
>        Class1 * myClass1;
>        ...
>    };
> Thus in class2.hpp, I include "class1.hpp".
>
> MY PROBLEM IS: THIS NEW SITUATION WILL NOT COMPILE!!!
> WHY???
>
> Thanks in advance,
> Bramz
>
>
>


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019