You also need a forward declaration of Observer before Subjectīs class definition because itīs unknown to the Subject class.
A forward declaration cannot replace a class definition. In this case the definition of Observer must be known in the implementation of Subject's notify method
A forward declaration cannot replace a class definition. In this case the definition of Observer must be known in the implementation of Subject's notify method
A simple way is to define the classes (e.g., in header files) without defining their member functions, then define their member functions after both classes have been defined (e.g., in source files). If you really want to define the member functions inline with their class definitions, then pick one class and define all its member functions inline. For the other class, define inline all those member functions that do not need the definition of the first class. Now, you can place the second class' definition before the first class' definition, and define the second class' remaining member functions separately (e.g., in a source file).
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Well I first told you how to correct the problem and then I told the others why their suggestions were wrong.
Anyway it's usually better to organize code according to the traditional .h and .cpp system. But I also sometimes do as you did, putting everything on one file for ease of testing.
In both cases it's important to know whether a forward declaration is enough or if the full definition must be known. It determines the order of things.
Last edited by nuzzle; September 28th, 2009 at 07:20 PM.
Bookmarks