title: How can I update c++ class members with a function?
body: <p>To describe the problem, I am trying to use objects in my code to stream line a three body problem.  I have the following code for the object:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;stdlib.h&gt;
  #include &lt;cstdio&gt;
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;vector&gt;
  #include &quot;star.h&quot;
  
  using namespace std;
  
  Star::Star( double m, double x_p, double y_p, double x_v, double y_v )
  {
      init( m, x_p, y_p, x_v, y_v);
  }
  
  void Star::init( double m, double x_p, double y_p, double x_v, double y_v )
  {
      Mass = m;
      X_Position = x_p;
      Y_Position = y_p;
      X_Velocity = x_v;
      Y_Velocity = y_v;
      R_Position[0] = X_Position;
      R_Position[1] = Y_Position;
      R_Velocity[0] = X_Velocity;
      R_Velocity[1] = Y_Velocity;
  }
  
  double Star::potential( Star star2, double dx, double dy )
  {
      double G = 3.0548e34;
      double Potential;
  
      double x_component = X_Position - star2.X_Position + dx;
      double y_component = Y_Position - star2.Y_Position + dy;
  
      double R = sqrt(x_component*x_component + y_component*y_component);
  
      Potential = G* Mass* star2.Mass / R;
      return Potential;
  }
  
  double * Star::compute_forces( Star star2 )
  {
      double h_x = ( X_Position - star2.X_Position )/1000;
      double h_y = ( Y_Position - star2.Y_Position )/1000;
  
      double *F = new double[2];
  
      F[0] = ( potential( star2, h_x, 0.0 ) - potential( star2, -h_x, 0.0 ) )/2*h_x;
      F[1] = ( potential( star2, 0.0, h_y ) - potential( star2, 0.0, -h_y ) )/2*h_y;
  
      return F;
  }
  
  void Star::verlet( Star star2, double h )
  {
      double *Force = compute_forces( star2 );
  
      X_Position += h*X_Velocity + 0.5*h*h*Force[ 0 ];
      Y_Position += h*Y_Velocity + 0.5*h*h*Force[ 1 ];
  
      double *Force_new = compute_forces( star2 );
  
      X_Velocity += 0.5*h*(Force[ 0 ] + Force_new[ 0 ] );
      Y_Velocity += 0.5*h*(Force[ 1 ] + Force_new[ 1 ] );
  }
  </code></pre>
  <p>Now I believe that the velocity verlet algorithm is correct, but when I run the code using this main file:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cmath&gt;
  #include &lt;cstdio&gt;
  #include &quot;star.h&quot;
  
  using namespace std;
  
  int main()
  {
      Star star1( 50, 0.0, 0.0, 0.0, 0.0 );
      Star star2( 1.00, 0.0, 1.0, -1.0, 1.0 );
      Star star3( 1.00, 0.0, -1.0, 1.0, 1.0 );
  
      Star arr[3] = { star1, star2, star3 };
  
      double h = 10/1000;
  
      //for ( double time = 0.0; time &lt;= 10.0; )
      //{
          for ( int inst = 0 ; inst&lt; 3; ++inst )
          {
              for ( int jnst = 0; jnst &lt; 3; ++jnst )
              {
                  if ( inst != jnst )
                  {
                      arr[ inst ].verlet( arr[ jnst ], h );
                      double *pos = arr[ inst ].get_positions();
                      cout &lt;&lt; &quot; &quot; &lt;&lt; pos[ 0 ] &lt;&lt; &quot; &quot; &lt;&lt; pos[ 1 ] &lt;&lt; endl;
  
                  }
              }
  
                      }
          //time += h;
      //}
  
      return 0;
  }
  </code></pre>
  <p>The values of members of the Star object are not updating :/. Is there something I am missing?
  the out put of the cout is this:</p>
  <pre><code> 0 0
   0 0
   0 1
   0 1
   0 -1
   0 -1
  </code></pre>
  <p>Thank you in advance!</p>
  <p>Edit:</p>
  <p>I tried implementing a <code>std::vector&lt;double&gt;</code> for my forces, but I ended up with a segmentation fault.</p>
  <p>Edit 2:
  After checking my <code>get_positions()</code> method I noticed it was returning only the initialized values. So I tried implementing this:</p>
  <pre class="lang-cpp prettyprint-override"><code>std::vector&lt;double&gt; get_positions(){  std::vector&lt;double&gt; temp = { X_Position , Y_Position }; return temp; }
  </code></pre>
  <p>And it worked so i implemented the following into my main code.</p>
  <pre class="lang-cpp prettyprint-override"><code>        std::vector&lt;double&gt; p1 = star1.get_positions();
          std::vector&lt;double&gt; p2 = star2.get_positions();
          std::vector&lt;double&gt; p3 = star3.get_positions();
  
          cout &lt;&lt; p1[ 0 ] &lt;&lt; &quot; &quot; &lt;&lt; p1[ 1 ] &lt;&lt; &quot; &quot;  &lt;&lt; p2[ 0 ] &lt;&lt; &quot; &quot; &lt;&lt; p2[ 1 ] &lt;&lt; &quot; &quot; &lt;&lt; p3[ 0 ] &lt;&lt; &quot; &quot; &lt;&lt; p3[ 1 ] &lt;&lt; endl;
  </code></pre>
  <p>However now I am stuck on a completely new problem... Now I am getting the following numbers for the algorithm updates!</p>
  <pre><code>5.66002e-320 2.31834e-316
  1.132e-316 4.63669e-313
  1.698e-319 6.95503e-316
  1.132e-316 4.63669e-313
  5.66002e-320 2.31834e-316
  1.132e-316 4.63669e-313
  1.698e-319 6.95503e-316
  1.132e-316 4.63669e-313
  5.66002e-320 2.31834e-316
  1.132e-316 4.63669e-313
  1.698e-319 6.95503e-316
  1.132e-316 4.63669e-313
  </code></pre>
  <p>Which means some where I am multiplying by zeros somewhere in my code. The problem is I cant for the life of me see where. Thanks if there is any help!</p>