Talk About Network



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++ > Forward referen...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 45741 of 45898
Post > Topic >>

Forward reference compiler error...

by "barcaroller" <barcaroller@[EMAIL PROTECTED] > May 3, 2008 at 10:03 AM

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;
        }
    }


The compiler error I get is (note the use of 'struct' not 'class'):

    error: invalid use of undefined type 'struct A'
    error: forward declaration of 'struct A'


When I place the class B definition after class A, the error goes away.  I
thought it was okay to use a "pointer" to an object of a
forward-referenced
class.




 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 Wed May 14 6:48:28 CDT 2008.