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 5 of 7 Topic 45741 of 47032
Post > Topic >>

Re: Forward reference compiler error...

by "Jim Langston" <tazmaster@[EMAIL PROTECTED] > May 3, 2008 at 09:03 PM

barcaroller wrote:
> "Pete Becker" <pete@[EMAIL PROTECTED]
> wrote in message
> news:2008050310124475249-pete@[EMAIL PROTECTED]
>>
>> At the point of "new A" the compiler has to generate code to create
>> an object of tpye A. Since it hasn't seen the definition of A, it
>> can't do that.
>
>
> Thank you for your response.  Actually, it's not the new() that is
> causing me problems.  Let me present the problem in another way.
>
>
>    class A;  // forward reference
>
>    class B
>    {
>        int print()
>        {
>
>        }
>
>        foo(A* a)
>        {
>            cout << a->print();  // compiler error here
>        }
>    }
>
>    class A
>    {
>        int print()
>        {
>
>        }
>
>        bar(B* b)
>        {
>            cout << b->print();
>        }
>    }
>
>    main()
>    {
>        a = new A;
>        b = new B;
>
>        b->foo(a);
>        a->bar(b);
>    }
>
>
> Basically, I have two objects that are dependent on each other.  I
> thought I would be okay as long as I use pointers to these objects. Is 
> there a way around this problem (other than re-designing)?

Ouput of program is
21

#include <iostream>

class A;  // forward reference

class B
{
public:
    int print()
    {
        return 1;
    }

    void foo(A* a);
};

class A
{
public:
    int print()
    {
        return 2;
    }

    void bar(B* b)
    {
        std::cout << b->print();
    }
};

void B::foo(A* a)
{
    std::cout << a->print();
}

main()
{
    A* a = new A;
    B* b = new B;

    b->foo(a);
    a->bar(b);

    delete a;
    delete b;
}




-- 
Jim Langston
tazmaster@[EMAIL PROTECTED]

 




 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 Fri Jul 25 21:53:35 CDT 2008.