Monday, June 22, 2009

C++ forward declaration error


I am trying to declare and use a class B inside of a class A and define B outside A.I know for a fact that this is possible because Bjarne Stroustrup uses this in his book "The C++ programming language" (page 293,for example the String and Srep classes). So this is my minimal piece of code that causes problems.

class A
struct B; // forward declaration
B* c; A()

struct A::B
/** we define struct B like this
** becuase it was first declared
** in the namespace A */

int i;
int main()



This code gives the following compilation errors in g++ : tst.cpp: In constructor ‘A::A()’: tst.cpp:5: error: invalid use of undefined type ‘struct A::B’ tst.cpp:3: error: forward declaration of ‘struct A::B’


Define the constructor for A AFTER the definition of struct B.

No comments:

Post a Comment