Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Forward ref...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 7 Topic 45741 of 48020
Post > Topic >>

Re: Forward reference compiler error...

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.
 




 7 Posts in Topic:
Forward reference compiler error...
"barcaroller" &  2008-05-03 10:03:33 
Re: Forward reference compiler error...
Pete Becker <pete@[EMA  2008-05-03 10:12:44 
Re: Forward reference compiler error...
"barcaroller" &  2008-05-03 10:36:50 
Re: Forward reference compiler error...
Pete Becker <pete@[EMA  2008-05-03 10:45:20 
Re: Forward reference compiler error...
"Jim Langston"   2008-05-03 21:03:01 
Re: Forward reference compiler error...
Jerry Coffin <jcoffin@  2008-05-03 10:34:28 
Re: Forward reference compiler error...
Andrey Tarasevich <and  2008-05-03 09:53:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 9:04:20 CDT 2008.