Sorry to ask a really basic programming question, but it's driving me
mad! I am sure somebody knows the answer.

Trying to get two mutually linked classes in eVC3. Sometimes it will
compile, sometimes it will not. No idea why. This seems to work sometimes:

-------------------foo.cpp-----------------------
#include "foo.h"
-------------------foo.h-------------------------
#pragma once
class foo;
#include "bar.h"
class foo { public: bar *p; };
-------------------bar.h-------------------------
#pragma once
class bar;
#include "foo.h"
class bar { public: foo *p; };
-------------------------------------------------

And sometimes I'll get:

Compiling...
foo.cpp
bar.h(4) : error C2079: 'p' uses undefined class 'foo'

Can somebody suggest some probably obvious reason why this model
sometimes will compile, and sometimes will not. Or suggest a better, or
correct, method in eVC3 to mutually link classes.

Thanks and regards, Ben.

Re: Cyclic references in classes by Charles

Charles
Thu Feb 05 13:46:35 CST 2004

I'm not sure why that would sometimes work and sometimes not, but you might
try this:
> -------------------foo.h-------------------------
> #pragma once
> #include "bar.h"
> class bar; // pre-declare bar
> class foo { public: bar *p; };
> -------------------bar.h-------------------------
> #pragma once
> #include "foo.h"
> class foo; // pre-declare foo
> class bar { public: foo *p; };
> -------------------------------------------------

--
This posting is provided "AS IS" with no warranties, and confers no rights.

Charles Schweizer [MSFT]
Software Design Engineer
Mobile Devices Product Group

This information is provided "AS IS" with no warranties, and confers no
rights.
"Ben Clewett" <ben@roadrunner.uk.com> wrote in message
news:402249aa.0@news.roadtech...
> Sorry to ask a really basic programming question, but it's driving me
> mad! I am sure somebody knows the answer.
>
> Trying to get two mutually linked classes in eVC3. Sometimes it will
> compile, sometimes it will not. No idea why. This seems to work
sometimes:
>
> -------------------foo.cpp-----------------------
> #include "foo.h"
> -------------------foo.h-------------------------
> #pragma once
> class foo;
> #include "bar.h"
> class foo { public: bar *p; };
> -------------------bar.h-------------------------
> #pragma once
> class bar;
> #include "foo.h"
> class bar { public: foo *p; };
> -------------------------------------------------
>
> And sometimes I'll get:
>
> Compiling...
> foo.cpp
> bar.h(4) : error C2079: 'p' uses undefined class 'foo'
>
> Can somebody suggest some probably obvious reason why this model
> sometimes will compile, and sometimes will not. Or suggest a better, or
> correct, method in eVC3 to mutually link classes.
>
> Thanks and regards, Ben.
>
>



Re: Cyclic references in classes by Ben

Ben
Fri Feb 06 07:10:38 CST 2004

Charles,

Thanks, this works perfectly.

Ben.

Charles Schweizer [MSFT] wrote:

> I'm not sure why that would sometimes work and sometimes not, but you might
> try this:
>
>>-------------------foo.h-------------------------
>>#pragma once
>>#include "bar.h"
>>class bar; // pre-declare bar
>>class foo { public: bar *p; };
>>-------------------bar.h-------------------------
>>#pragma once
>>#include "foo.h"
>>class foo; // pre-declare foo
>>class bar { public: foo *p; };
>>-------------------------------------------------
>
>