Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > n00b class question

Reply
Thread Tools

n00b class question

 
 
thekestrel@gmail.com
Guest
Posts: n/a
 
      11-10-2006
Why can't I create an instance of A and B in the Class C definition?

#include <iostream>

class B;
class A;

class C {
public:
B b;
A a;
};

class B {
int b;
};

class A {
int a;
};

int main (void)
{
C c;

return 0;
}

 
Reply With Quote
 
 
 
 
Kavya
Guest
Posts: n/a
 
      11-10-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-10-2006
(E-Mail Removed) wrote:
> Why can't I create an instance of A and B in the Class C definition?
>
> #include <iostream>
>
> class B;
> class A;
>
> class C {
> public:
> B b;
> A a;
> };


To understand how to construct an instance of C, the compiler needs to
know what the internal structures of 'B' and 'A' are. Since you didn't
define (only declared) the classes 'B' and 'A' before 'C', the compiler
cannot do what it needs. Move the definition of 'C' _after_ 'B' and 'A'
definitions.

>
> class B {
> int b;
> };
>
> class A {
> int a;
> };
>
> int main (void)
> {
> C c;
>
> return 0;
> }


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


 
Reply With Quote
 
thekestrel@gmail.com
Guest
Posts: n/a
 
      11-10-2006

Kavya wrote:
> (E-Mail Removed) 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.

AwooOOoo

 
Reply With Quote
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      11-10-2006
(E-Mail Removed) 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]

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-10-2006
(E-Mail Removed) 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


 
Reply With Quote
 
thekestrel@gmail.com
Guest
Posts: n/a
 
      11-10-2006

Scott McPhillips [MVP] wrote:
> (E-Mail Removed) 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.

Appreciate your thoughts.

AwooOOoo.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-10-2006
(E-Mail Removed) wrote:

>
> Kavya wrote:
>> (E-Mail Removed) 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
 
Reply With Quote
 
gangs
Guest
Posts: n/a
 
      11-10-2006
Hi,
Does this help:

class B;
class A
{
B* b;
};

class B
{
A* a;
};

Regards,
Tushar.

Kai-Uwe Bux wrote:

> (E-Mail Removed) wrote:
>
> >
> > Kavya wrote:
> >> (E-Mail Removed) 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


 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      11-10-2006

(E-Mail Removed) wrote:
> Why can't I create an instance of A and B in the Class C definition?
>
> #include <iostream>
>
> class B;
> class A;
>
> class C {
> public:
> B b;
> A a;
> };
>
> class B {
> int b;
> };
>
> class A {
> int a;
> };
>
> int main (void)
> {
> C c;
>
> return 0;
> }


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...

/* ___ c.hpp ___ class C interface declaration */
#ifndef C_HPP_
#define C_HPP_ /* include guard */

#include "a.hpp"
#include "b.hpp"

class C
{
A a;
B b;
public:
C(); // def ctor
};

#endif /* include guard C_HPP_ */

/* ___ c.cpp ___ class C implementation */
#include "c.hpp"

// def ctor
C::C() : a(0), b(0)
{
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
n00b question: Possible to pass lists to a Template class? Wells Python 2 05-13-2009 10:20 PM
n00b question re= WebClient class Chris Dunaway ASP .Net 2 09-08-2006 01:04 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
n00b Question re: MSDE SQL 2000 Set-up newgenre ASP .Net 2 04-19-2004 06:16 PM
ultimate n00b question re: my / our Koncept Perl 3 12-06-2003 10:58 PM



Advertisments