by Jerry Coffin <jcoffin@[EMAIL PROTECTED]
>
May 3, 2008 at 10:34 AM
In article <fvhrd0$2fd$1@[EMAIL PROTECTED]
>, barcaroller@[EMAIL PROTECTED]
says...
> Why does the following code cause a compiler error?
>
>
> class A; // forward reference
>
> class B
> {
> foo()
> {
> a = new A;
>
> cout << a->bar(); // compiler error here
> }
>
> A* a;
> }
>
> class A
> {
> int bar()
> {
> return 1;
> }
> }
Because when you declare (but don't define) the class, it's an
incomplete type. You can create a pointer (or reference) to that type,
but anything that involves _dereferencing_ the pointer (among other
things) needs a _definition_ of the class.
For the compiler to handle something like 'a->b()', it has to have seen
declarations for both 'a' and 'b'. Using a reference would be the same
-- for you to use something like 'x.y()', it has to have seen
declarations of both 'x' and 'y'.
--
Later,
Jerry.
The universe is a figment of its own imagination.