Note that there are some explanatory texts on larger screens.

plurals
  1. PODealing with circular dependency on inheritance
    primarykey
    data
    text
    <p>I'm gonna go C++ über n00b on this one and ask how is the best way to deal with a circular dependency when you have inheritance.</p> <p>The set is simple: Scene class extends Actor; Scene has a pointer to a vector of Actors; Actor has a pointer for (parent) Scene.</p> <p>As for include files I got:</p> <p>Scene.h:</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; using namespace std; #ifndef __Scene_h__ #define __Scene_h__ #include "Actor.h" namespace myns { // class Actor; class Scene; } namespace myns { class Scene: public myns::Actor { /* private class attributes... */ public: /* public class attributes... */ std::vector&lt;myns::Actor*&gt; actors; Scene(/* arguments */); /* public class methods... */ }; } #endif </code></pre> <p>Actor.h</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; using namespace std; #ifndef __Actor_h__ #define __Actor_h__ #include "Scene.h" namespace myns { // class Scene; class Actor; } namespace myns { class Actor { /* private class attributes... */ public: /* public class attributes... */ myns::Scene* scene; Actor(); Actor(/* arguments */); /* public class methods... */ }; } #endif </code></pre> <p>But this gives me alot of C2504 errors/base class undefined on Visual Studio 2010.</p> <p>If I comment the Scene.h include on the Actor.h and uncomment the forward declaration of Scene on Actor.h it works, but then, in my app, if I want to include only the Actor.h on a particular piece of code, it will not work. How can I put this to work while maintaining the inclusion independence for Actor.h - including Actor.h without the need of previously manually including Scene.h?</p> <p>What is wrong with my class definitions and how is the best way to deal with this circular dependency?</p> <p>Shouldn't the #ifndef directives prevent this inclusion problem?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload