Forward reference compiler error...

B

barcaroller

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

barcaroller

Pete Becker said:
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)?
 
J

Jerry Coffin

[email protected] says... said:
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'.
 
J

Jim Langston

barcaroller said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top