Pointer problem when using forward class declarations

I'm writing some test code to get my head around various elements I'm not fluent with yet. I have two classes which references each other through pointers, and to solve that I used forward declarations of the classes, which compiles and runs nicely. However, whenever I try to actually use the pointers in any way, the compiler (GCC 4.2) returns the following errors:

"Forward declaration of struct Foo"
"Invalid use of incomplete type 'struct Foo'"

Here's the line that makes all the noise:

d->sayHello();

In which d is a pointer to an instance of the other class. What is wrong here? And why does gcc suddenly think that the class is a struct?

Thanks,
Fafner
Structs and Classes are pretty much equivalent in C++. (the struct defines his member as public in default, class does this as private)

If you declare a Class but not define it, you can use Foo as a type. But if you want to access any members (or methods) of the class the compiler needs more information than just the forward declaration.

You need to include a header file with a proper declaration. (or similar)


Maikel
Ok, thanks. I haven't been writing in c++ for too long, so I didn't know that about structs and classes, thank you ;)

I included the header files with the declarations like you said, and now it works!

Thanks you!
Topic archived. No new replies allowed.