title: C++ Forward declaration and destructor
body: <p>Two of my classes had to include each other. I made forward declarations instead, compilation is ok. One function of these classes is to call the destructor of the other. And that the compiler spits warnings at me, the destructor will not be called. What can I do? I can avoid this problem by creating another class for the function I need, avoiding the forward declarations but that would not be educative for me...</p>
  
  <p>Here is my first class Header.h :</p>
  
  <pre><code>#ifndef H_HEADER
  #define H_HEADER
  
  #include "SDL/SDL.h"
  #include "SDL/SDL_image.h"
  #include "SDL/SDL_ttf.h"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;sstream&gt;
  #include "DataFiles.h"
  
  class Character; // forward declaration  Header &lt;-&gt; Character
  
  class Header {
  
  private:
   Character * ch; 
  };
  
  void cleanUp(std::vector &lt;SDL_Surface*&gt; &amp; Vsurface, std::vector &lt;TTF_Font*&gt; &amp; Vfont, std::vector &lt;Character*&gt; &amp; Vchar);
  
  // ... Other functions use in main.cpp
  
  #endif
  </code></pre>
  
  <p>HEre is the Header.cpp:</p>
  
  <pre><code>#include "Header.h"
  using namespace std;
  
  
  void cleanUp(vector &lt;SDL_Surface*&gt; &amp; Vsurface, vector &lt;TTF_Font*&gt; &amp; Vfont, vector &lt;Character*&gt; &amp; Vchar) {
  
   for(unsigned int i(0); i &lt; Vsurface.size(); i++) 
    SDL_FreeSurface(Vsurface[i]);
   for(unsigned int i(0); i &lt; Vfont.size(); i++)
    TTF_CloseFont(Vfont[i]);
   for(unsigned int i(0); i &lt; Vchar.size(); i++)
    delete Vchar[i];
  
   TTF_Quit();
   SDL_Quit();
  
  }
  </code></pre>
  
  <p>And here is the other Character.h class:</p>
  
  <pre><code>#ifndef H_CHARACTER
  #define H_CHARACTER
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;vector&gt;
  #include &lt;/usr/include/SDL/SDL_image.h&gt;
  #include &lt;/usr/include/SDL/SDL.h&gt;
  #include &lt;/usr/include/SDL/SDL_ttf.h&gt;
  
  #include "DataFiles.h"
  #include "CharFrame.h"
  
  class Header; // Forward declaration  Header &lt;-&gt; Character
  
  class Character {
  
  public:
   Character(std::string&amp; dataPath);
   ~Character();
   // .. other functions 
  
  private:
  
   Header * h;
   // ... other attributes
  };
  #endif
  </code></pre>
  
  <p>And here is my Character destructor:</p>
  
  <pre><code>Character::~Character() {
  
   cout &lt;&lt; "Character " &lt;&lt; m_name &lt;&lt; " deleted.\n-----------------------------------\n" &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>So when my program ends, I call upon the Header's function "cleanUp()" giving it a vector of pointers to Characters. Every pointer should then be deleted through the Character's destructor ~Character();
  However compilation gives me three warnings:</p>
  
  <pre><code>Header.cpp: In function ‘void cleanUp(std::vector&lt;SDL_Surface*&gt;&amp;, std::vector&lt;_TTF_Font*&gt;&amp;, std::vector&lt;Character*&gt;&amp;)’:
  Header.cpp:66:17: warning: possible problem detected in invocation of delete operator: [enabled by default]
  Header.cpp:66:17: warning: invalid use of incomplete type ‘struct Character’ [enabled by default]
  Header.h:27:7: warning: forward declaration of ‘struct Character’ [enabled by default]
  Header.cpp:66:17: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
  </code></pre>
  
  <p>And once my program terminates, the character's destructor's message won't show up which means the destructor clearly isn't called.</p>
  
  <p>What am I doing wrong with the forward declarations?</p>