base class containing pointer of derived type
<p>I'm trying to model file objects in C++. I view all file objects as "nodes" represented by a CNode class. These nodes can be either files or directories represented by CFile and CDirectory respectively which inherit from the CNode class.</p>

<p>Regardless of the type of node a node is, I want it to have a handle to the CDirectory object that represents the parent directory of the node. So there's 2 views that shouldn't be confused:
1. the programming view which sees CFile and CDirectory inheriting from parent class CNode
2. the real world view which sees files and directories located in a parent directory (fyi, files/directories located at root would have a NULL CDirectory handle for obvious reasons)</p>

<p>The big idea is that when a CFile object or CDirectory object is instantiated with a path, for example, /path/to/my/file.txt, if the "to" directory doesn't exist, it will be created along with the sub-directories and file named in the path. On destruction, the directories/file that were created by the object will be deleted leaving the file system in the state it was in before using these objects- on one condition. The condition is that if the file "file.txt," in this example, was written to, "file.txt" and the directories that were created in the process of creating "file.txt" will not be deleted.
To implement this behavior, the CFile object representing "file.txt" will use the CDirectory * that it inherits from its base class (CNode) and invoke its "setWrittenToFlag()" which will recursively call the same method for the parent's parent directory and so on. This is a method that is only included in the CDirectory class so its imporant for each node to have a handle to its parent directory so that it can invoke this method.</p>

<p>Everything has been coded but I can't get it to compile for one reason as demonstrated by this code:</p>

<p>CNode.hpp:</p>

<pre><code>#ifndef _CNode_HPP_
#define _CNode_HPP_

#include "CDirectory.hpp"


class CDirectory;

class CNode
{
private:
    CDirectory * b;
};

#endif
</code></pre>

<p>CDirectory.hpp:</p>

<pre><code>#ifndef _CDirectory_HPP_
#define _CDirectory_HPP_

#include "CNode.hpp"


class CDirectory : public CNode
{
};

#endif
</code></pre>

<p>main.cpp:</p>

<pre><code>#include &lt;iostream&gt;
#include "CNode.hpp"
#include "CDirectory.hpp"


int main()
{
    CDirectory dir;

    return EXIT_SUCCESS;
}
</code></pre>

<p>This produces the following compilation error:</p>

<pre><code>CDirectory.hpp:8: error: expected class-name before ‘{’ token
</code></pre>

<p>Combining the 2 .hpp files gets the code to compile but in keeping with standards, I would like to keep them separated. How can I get this working? Is there a better design? I'm looking for elegant OOD solutions only- I don't want to cast unless it makes sense to.
Assume that each class has methods that are specific to that class/node-type and so I don't want to combine classes or anything like that.</p>

<p>EDIT:
Thanks for the quick response guys! The solution turned out to be to remove the #include "CDirectory.hpp" from the CNode.hpp since the forward declaration is there but this causes issues in the .cpp files that were including "CNode.hpp" and subsequently "CDirectory.hpp" since "CDirectory.hpp" is no longer there- so you have to add that #include "CDirectory.hpp" line to those individual .cpp files.</p>