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