title: C++ : class A reference classe B (in a vector) and class B reference A (only one)
body: <p><strong>LAST EDIT: FINAL CODE LIES AT TH END OF THE MESSAGE</strong></p>
  
  <p>i am coming from Java and I am looping for a while :</p>
  
  <p>I try to implement navigability from classes Zoo to Animal and Animal to Zoo.</p>
  
  <p><strong>Q.1) did I write it the right way ? (source code on bottom)</strong></p>
  
  <p>I have got an compile error related to "stl_vector.h"
  class Zoo : animals.push_back(a); 
  with message : "error:cannot increment a pointeur to incomplete type Animal"</p>
  
  <p><strong>Q.2 ) is it related to the way I implemented navigability ?</strong></p>
  
  <p>In the main.cpp, i will add animals to the zoo :</p>
  
  <pre><code>Zoo z();
  std::string nom("sheeta");
  Animal a(z,nom);
  </code></pre>
  
  <p><strong>Q.3) the Animal constructor exists ... : why does the compiler tell me that</strong></p>
  
  <p>compile error : "no matching function for call Animal::Animal(Zoo (&amp;)(), std::string &amp;)</p>
  
  <p><strong>Q.4) what is the c++ way to write :</strong></p>
  
  <pre><code>z.addAnimal(Animal(z,nom));
  </code></pre>
  
  <p><strong>EDIT1 : here is what i want to implement : a zoo contains n animals, an animal knows which zoo it belongs to</strong></p>
  
  <p><strong>LAST EDIT : picture updated with your help</strong>
  <strong>==========================================</strong></p>
  
  <p><a href="https://i.stack.imgur.com/z0Yox.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z0Yox.png" alt="enter image description here"></a></p>
  
  <p>many thanks for helping !</p>
  
  <hr>
  
  <p>Here are the changes i made from your precious comments : </p>
  
  <p><strong>LAST EDIT IN SOURCE CODE - 2015-12-18 07:20 gmt</strong>
  <strong>===============================================</strong></p>
  
  <p>file Zoo.h</p>
  
  <pre><code>#ifndef ZOO_H
  #define ZOO_H
  
  #include &lt;vector&gt;
  </code></pre>
  
  <p>** ADDED : include Animal.h **</p>
  
  <pre><code>#include "Animal.h"
  
  class Zoo
  {
    private:
      std::string zooName; // no importance : name of the zoo
      std::vector&lt;Animal&gt; animals;  // list of animals
    public:
      Zoo(std::string  n); // no importance : name of the zoo
  </code></pre>
  
  <p>** ADDED : const &amp; **</p>
  
  <pre><code>    void addAnimal(Animal const &amp; a); 
      std::vector&lt;Animal&gt;  getAnimals();
  };
  
  #endif
  </code></pre>
  
  <p>and Zoo.cpp :</p>
  
  <pre><code>#include "Zoo.h"
  </code></pre>
  
  <p>** REMOVED : include Animal.h</p>
  
  <pre><code>//#include "Animal.h"
  
  Zoo::Zoo(std::string  n) // no importance, n is the name of the zoo
      {
          zooName = n;
  </code></pre>
  
  <p>** REMOVED : **</p>
  
  <pre><code>        //std::vector&lt;Animal&gt; animals(); // &lt;&lt;&lt; REMOVED
      }
  </code></pre>
  
  <p>** ADDED : const &amp; **</p>
  
  <pre><code>void Zoo::addAnimal(Animal const &amp;a)    // &lt;&lt;&lt;&lt; const added
      {
          animals.push_back(a); // add an animal to the zoo
      }
  
  std::vector&lt;Animal&gt; Zoo:: getAnimals()
      {
          return animals;
      }
  </code></pre>
  
  <p>And Animal.h</p>
  
  <pre><code>#ifndef ANIMAL_H
  #define ANIMAL_H
  #include &lt;string&gt;
  </code></pre>
  
  <p>** ADDED : forward declaration of Zoo **</p>
  
  <pre><code>class Zoo;
  </code></pre>
  
  <p>** REMOVED : include file</p>
  
  <pre><code>// #include "Zoo.h"    
  
  class Animal
  {
    private:
  </code></pre>
  
  <p>** CHANGED : pointer to Zoo **</p>
  
  <pre><code>    Zoo * zoo; // in which zoo   &lt;&lt;&lt; CHANGED TO A POINTER
      std::string nom;  // name of an animal
  
    public:
  </code></pre>
  
  <p>** CHANGED : pointer to Zoo AND REMOVE &amp; after string**</p>
  
  <pre><code>    // Animal(Zoo z, std::string &amp; n); 
      Animal(Zoo * z, std::string n); 
      std::string toString();
  };
  
  #endif
  </code></pre>
  
  <p>And Animal.cpp :</p>
  
  <pre><code>#include "Animal.h"
  </code></pre>
  
  <p>** ADDED : include "Zoo.h"**</p>
  
  <pre><code>#include "Zoo.h"
  #include &lt;sstream&gt; // ADDED TO CONVERT number to string
  </code></pre>
  
  <p>** CHANGED : pointer to Zoo AND REMOVED &amp; after string**</p>
  
  <pre><code>//Animal::Animal(Zoo  z, std::string &amp; n) // 
   Animal::Animal(Zoo * z, std::string  n) // 
      {  zoo = z; nom= n;
      }
  
  std::string Animal::toString()
      {
  </code></pre>
  
  <p>** CHANGED : to justify the use of Zoo inside Animal **</p>
  
  <pre><code>    std::string msg1 (" among ");
      std::string msg3 (" other animals") ;
      // to convert number to string
          std::ostringstream chaine;
          chaine &lt;&lt; zoo-&gt;getAnimals().size();
      std::string msg2(chaine.str());
  
      return (name+msg1+msg2+msg3);
  
      }
  </code></pre>
  
  <hr>
  
  <p>EDIT3 - And a main function to test these classes :</p>
  
  <pre><code> #include &lt;iostream&gt;
   #include "Animal.h" 
   #include "Zoo.h"
   int main()
  {
      std::string zooName("BigZoo");
      Zoo z(zooName);
      std::string nom;
  
      std::cin &gt;&gt; nom;
      while (nom.compare("*") != 0)
      {
  </code></pre>
  
  <p>**CHANGED : 2 lines deleted and 1 line added</p>
  
  <pre><code>    //    Animal a(&amp;z,nom);
      //    z.addAnimal(a);
          z.addAnimal(Animal(&amp;z, nom));
          std::cin &gt;&gt; nom;
      }
  
      for (unsigned int i=0;i&lt;z.getAnimals().size(); i++)
      { // for each animal of the zoo
          std::cout &lt;&lt; z.getAnimals().at(i).toString() &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>And a test to end : </p>
  
  <pre><code>&gt;max 
  &gt;bill
  &gt;jumbo
  &gt;sheeta
  &gt;*
   max among 4 other animals
   bill among 4 other animals
   jumbo among 4 other animals
   sheeta among 4 other animals
  </code></pre>
  
  <p><strong>well done, stackoverflow is really great !!! thanks again</strong></p>
  
  <hr>
  
  <p><strong>=====================================</strong></p>
  
  <p><strong>FINAL HEADER CODE</strong></p>
  
  <p><strong>=====================================</strong></p>
  
  <pre><code>/* Zoo.h */
  #ifndef ZOO_H
  #define ZOO_H
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include "Animal.h"
  
  class Zoo
  {
    private:
      std::string zooName;
      std::vector&lt;Animal&gt; animals;  // liste of animals
    public:
      Zoo(std::string  n);
      void addAnimal(Animal const &amp; a);
      std::vector&lt;Animal&gt;  getAnimals();
  };
  
  #endif
  
  
  /* Animal.h */
  
  #ifndef ANIMAL_H
  #define ANIMAL_H
  #include &lt;string&gt;
  
  class Zoo;
  
  class Animal
  {
    private:
      Zoo *zoo ; // in which zoo
      std::string name;  // name of an animal
  
    public:
      Animal(Zoo *z, std::string  n);
      std::string toString();
  };
  
  #endif
  </code></pre>