title: Change array value from another class in C++
body: <p>I'm getting Segmentation Fault error.  I'm fairly new to C++ so not really familiar with pointers and other such things.  This seems to be a fundamental aspect but i can't seem to figure it out, I've spent countless hours on it. </p>
  
  <p>I have 5 files element.h, element.cpp, heap.h, heap.cpp, main.cpp </p>
  
  <p>error occurs on the line </p>
  
  <blockquote>
    <p>h.setElements(e,i);</p>
  </blockquote>
  
  <p>Which is a function in Heap.cpp</p>
  
  <p>I know it has something to do with arrays</p>
  
  <p>MAIN.CPP</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "heap.h"
  #include "element.h"
  using namespace std;
  
  int main(){
    Heap h;
    h = h.initialize(3);
  
    for(int i=0;i&lt;h.getCapacity(); ++i){
      Element e;
      e.setKey(i);
      h.setElements(e,i); // Error occurs here
  }
  
     h.printHeap(h);
     return 0;
  }
  </code></pre>
  
  <p>HEAP.H</p>
  
  <pre><code>#ifndef heap_h
  #define heap_h
  #include "element.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  class Heap {
   public:
  
  
      Element* getElements();
      void setElements(Element e,int index);
      Heap initialize(int n);
      void printHeap(Heap heap);
  
  
    private:
         int capacity;
         int size;
         Element* H;
    };
  
  
    #endif
  </code></pre>
  
  <p>HEAP.CPP</p>
  
  <pre><code>#include "heap.h"
  
  
  Heap Heap::initialize(int n){
    H = new Element[n];
    Heap h;
    h.capacity = n;
    h.size = 0;
    return h;
  }
  
  
  void Heap::printHeap(Heap heap){
      for(int i=0;i&lt;heap.capacity;++i){
        cout &lt;&lt; "Element " &lt;&lt; i &lt;&lt; " = " &lt;&lt; H[i].getKey() &lt;&lt; endl;
      }
  }
  
  
   void Heap::setCapacity(int nCapacity ) {
     capacity = nCapacity;
   }
  
   int Heap::getCapacity(void) {
      return capacity;
   }
  
  void Heap::setSize(int nSize ) {
      size = nSize;
  }
  
   int Heap::getSize(void) {
      return size;
   }
   Element* Heap::getElements(void){
       return H;
    }
   void Heap::setElements(Element e,int index){
        H[index] = e;
   }
  </code></pre>