Guest

Kavya wrote:
> wrote:
> > Why can't I create an instance of A and B in the Class C definition?
>
> Because class A and B are not yet defined.

Thanks for the reply,
I thought that the

class B;
class A;

at the start essentially helped the compiler know the rest of the
definition would be later on?How can I have it know about the
definition of A and B whilst still having them listed after C? (for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.

wrote:
> Thanks for the reply,
> I thought that the
>
> class B;
> class A;
>
> at the start essentially helped the compiler know the rest of the
> definition would be later on?How can I have it know about the
> definition of A and B whilst still having them listed after C? (for a
> more complicated problem which essentially means A contains B and B
> contains A so reordering would not fix it.
>
> AwooOOoo
>

You cannot put B and C members in A unless B and C are defined first.
One reason is very fundamental: The compiler must know their size in
order to make them part of C.

You can, however, put pointers to B and C in A. The compiler knows what
size a pointer is

wrote:
> I thought that the
>
> class B;
> class A;
>
> at the start essentially helped the compiler know the rest of the
> definition would be later on?

You seem to be asking a question here. The answer is "yes, you do
seem to have thought that".
>How can I have it know about the
> definition of A and B whilst still having them listed after C?

"How can I have my cake and eat it too?"
> (for a
> more complicated problem which essentially means A contains B and B
> contains A so reordering would not fix it.

The closing parenthesis is missing.

This is precisely the point. The compiler has to know the sizes of
the objects A and B so it can generate proper code. This is the
requirement of the language, mostly so that the compilers don't have
to be too sophisticated (which often means unimplementable).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Guest

Scott McPhillips [MVP] wrote:
> wrote:
> > Thanks for the reply,
> > I thought that the
> >
> > class B;
> > class A;
> >
> > at the start essentially helped the compiler know the rest of the
> > definition would be later on?How can I have it know about the
> > definition of A and B whilst still having them listed after C? (for a
> > more complicated problem which essentially means A contains B and B
> > contains A so reordering would not fix it.
> >
> > AwooOOoo
> >
>
> You cannot put B and C members in A unless B and C are defined first.
> One reason is very fundamental: The compiler must know their size in
> order to make them part of C.
>
> You can, however, put pointers to B and C in A. The compiler knows what
> size a pointer is
>
> --
> Scott McPhillips [VC++ MVP]

Makes Sense. The example I'm playing with is to get familiar with
templates and the calling that I was trying to simplify is better
represented by,

template <class T>
class Balls {
public:
list<Sticks<T> *> s;
}

template <class T>
class Sticks {
public:
list<Balls<T> *> b;
}

So I want a list of pointers to the other templated class. I think this
means that I end up with a similar problem to what you described as s
and b are not pointers so the compiler doesn't know the size of the
list inside the class. Any thoughts on how to get around this? The idea
in the mock up would be that each class could contain a list of
pointers to the other class.

wrote:
>
> Kavya wrote:
>> wrote:
>> > Why can't I create an instance of A and B in the Class C definition?
>>
>> Because class A and B are not yet defined.
>
> Thanks for the reply,
> I thought that the
>
> class B;
> class A;
>
> at the start essentially helped the compiler know the rest of the
> definition would be later on?How can I have it know about the
> definition of A and B whilst still having them listed after C? (for a
> more complicated problem which essentially means A contains B and B
> contains A so reordering would not fix it.

You cannot have that. You will have to break the cycle by using an A* in B
or a B* in A (that is something for which a forwar declaration suffices!).

Think about it:

struct A {
int i;
B b;
};

struct B {
int j;
A a;
};

and ask yourself: will we have sizeof(A) < sizeof(B) because of the b in A
or will we have sizeof(B) < sizeof(A) because of the a in B? Obviously, you
would have both, which is a contradiction. Thus, you cannot do it.

Kai-Uwe Bux wrote:
> wrote:
>
> >
> > Kavya wrote:
> >> wrote:
> >> > Why can't I create an instance of A and B in the Class C definition?
> >>
> >> Because class A and B are not yet defined.
> >
> > Thanks for the reply,
> > I thought that the
> >
> > class B;
> > class A;
> >
> > at the start essentially helped the compiler know the rest of the
> > definition would be later on?How can I have it know about the
> > definition of A and B whilst still having them listed after C? (for a
> > more complicated problem which essentially means A contains B and B
> > contains A so reordering would not fix it.
>
> You cannot have that. You will have to break the cycle by using an A* in B
> or a B* in A (that is something for which a forwar declaration suffices!).
>
> Think about it:
>
> struct A {
> int i;
> B b;
> };
>
> struct B {
> int j;
> A a;
> };
>
> and ask yourself: will we have sizeof(A) < sizeof(B) because of the b in A
> or will we have sizeof(B) < sizeof(A) because of the a in B? Obviously, you
> would have both, which is a contradiction. Thus, you cannot do it.
>
>
> Best
>
> Kai-Uwe Bux

The above is expected unless you use pointers or references to A,B in
class C.
This should still be a minor issue since each class above will probably
end up being declared in its own header and implemented in its own
source file. At least, that is what your goal should be once you have
the skeleton up and running.
As in...

<> wrote in message
news:...
>
> Scott McPhillips [MVP] wrote:
>> wrote:
>> > Thanks for the reply,
>> > I thought that the
>> >
>> > class B;
>> > class A;
>> >
>> > at the start essentially helped the compiler know the rest of the
>> > definition would be later on?How can I have it know about the
>> > definition of A and B whilst still having them listed after C? (for a
>> > more complicated problem which essentially means A contains B and B
>> > contains A so reordering would not fix it.
>> >
>> > AwooOOoo
>> >
>>
>> You cannot put B and C members in A unless B and C are defined first.
>> One reason is very fundamental: The compiler must know their size in
>> order to make them part of C.
>>
>> You can, however, put pointers to B and C in A. The compiler knows what
>> size a pointer is
>>
>> --
>> Scott McPhillips [VC++ MVP]
>
>
> Makes Sense. The example I'm playing with is to get familiar with
> templates and the calling that I was trying to simplify is better
> represented by,
>
> template <class T>
> class Balls {
> public:
> list<Sticks<T> *> s;
> }
>
> template <class T>
> class Sticks {
> public:
> list<Balls<T> *> b;
> }
>
> So I want a list of pointers to the other templated class. I think this
> means that I end up with a similar problem to what you described as s
> and b are not pointers so the compiler doesn't know the size of the
> list inside the class.

No, that's not a problem. You're declaring a list which will hold pointers,
which is fine. The list size is dynamic; it grows when you add items to it.

Is that the actual code you have?

One problem is that you're missing semicolons ( after the class
definitions. Also, you'd need to forward declare "template <class T> class
Sticks;" before the Balls class. The it will compile fine.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!