title: How to make a template class a data member of another class?
body: <p>So what I'm trying to do is write a class with a template class as its data member. This is part of my homework.</p>
  
  <p>So far this is what I have:</p>
  
  <p>letter_frequencies.h</p>
  
  <pre><code>#include "ltr_freq_pair.h"
  #include "darray.h"
  
  class letter_frequencies
  {
      private:
          darray&lt;letterFrequencyStruct&gt; array;
      public:
          letter_frequencies();
          void outputFrequencies();
  };
  </code></pre>
  
  <p>letter_frequencies.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "letter_frequencies.h"
  
  letter_frequencies::letter_frequencies()
  {}
  
  void letter_frequencies::outputFrequencies()
  {
      for(int index = 0; index &lt; 26; index ++)
      {
          std::cout &lt;&lt; "\n" &lt;&lt; array[index].letter;
          std::cout &lt;&lt; " " &lt;&lt; array[index].frequency;
      }
  }
  </code></pre>
  
  <p>Everything compiles, but once I run the program it has this error:</p>
  
  <p><code>a.out: darray.cpp:44: T&amp; darray&lt;T&gt;::operator[](unsigned int) [with T = letterFrequencyStruct]: Assertion 'index &lt; used' failed.
  Abort (core dumped)</code></p>
  
  <p>The code snippet I'm trying to use for this class is:</p>
  
  <pre><code>    //Check if Class Works
      letter_frequencies array;
  
      array.outputFrequencies();
  </code></pre>
  
  <p>Right now, the darray works just fine and the program does exactly what I need it to do, but one of the requirements is to have a class named letter_frequency with a data member <code>darray&lt;structTypename&gt; name</code>, which is why I'm fussing with this in the first place.</p>
  
  <p>Also, how would I make the class call the explicit constructor for the darray? I want to set the size to 26.</p>
  
  <p>darray.h</p>
  
  <pre><code>#ifndef DARRAY_H
  #define DARRAY_H
  
  #include &lt;iostream&gt;
  
  template &lt;typename T&gt;
  class darray
  {
  
      public:
          //Constructors
          darray();
          darray(const darray&amp;);
          explicit darray(unsigned);
  
          //Destructor
          ~darray();
  
          //Constant Members
          unsigned size() const {return used;}
          unsigned capacity() const {return cap;}
  
          //Modification Members
          T&amp; operator [] (unsigned);
          void pop_back();
          void push_back(const T&amp;);
          const T* find(const T&amp;) const;
          void resize(unsigned);
  
          //Operators
          T&amp; operator = (const darray&amp;);
          bool operator == (const darray&amp;) const;
  
          //Friends
          template &lt;typename X&gt;
          friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; output, const darray&lt;X&gt;&amp; array);
  
      private:
          T* arr;
          unsigned used, cap;
  
  };
  
  template &lt;typename X&gt;
  std::ostream&amp; operator &lt;&lt; (std::ostream&amp; output, const darray&lt;X&gt;&amp; array)
  {
      for(int index = 0; index &lt; array.size; index++)
      {
          output &lt;&lt; index &lt;&lt; array[index];
      }
  
      return output;
  }
  
  #include "darray.cpp"
  
  #endif
  </code></pre>