Note that there are some explanatory texts on larger screens.

plurals
  1. POissues with Storing to array
    text
    copied!<p>I was testing one of my classes, but for some reason I can't seem to cast an intiger from a 2d array to double. Here is my (very simplified) code: In main.cpp</p> <pre><code> #include&lt;iostream&gt; #include&lt;conio.h&gt; #include&lt;string&gt; #include "trajectories.h" int main() { std::string response; int numOfCoords; int speed; int ** coords; std::cout&lt;&lt;"enter the number of coordinates: "; std::cin&gt;&gt;numOfCoords; std::cout&lt;&lt;"enter speed: "; std::cin&gt;&gt;speed; coords=new int *[numOfCoords]; for (int i=0; i&lt;numOfCoords; i++) coords[i] = new int[2]; for(int i=0; i&lt;numOfCoords*2; i++) { if(i%2==0) std::cout&lt;&lt;"enter point "&lt;&lt;i/2&lt;&lt;".x : "; else std::cout&lt;&lt;"enter point "&lt;&lt;i/2&lt;&lt;".y : "; std::cin&gt;&gt;coords[i/2][i%2]; } NPCTrajectory traj(numOfCoords, speed); traj.AddCoordinates(coords); std::cout&lt;&lt;coords[0][0]&lt;&lt;", "&lt;&lt;coords[0][1]&lt;&lt;std::endl; getch(); double currentCoords[2]; currentCoords[0]=double(coords[0][0]); currentCoords[1]=double(coords[0][1]); for(;;) { traj.HandleEvents(currentCoords); std::cout&lt;&lt;"current coordinates : ("&lt;&lt;currentCoords[0]&lt;&lt;", "&lt;&lt;currentCoords[1]&lt;&lt;")"&lt;&lt;std::endl; std::cout&lt;&lt;"do you wish to continue? "; getch(); } } </code></pre> <p>Trajectories.h contains class declaration only, so I believe it is irrelevant. Here is my trajectories.cpp</p> <pre><code> #include "trajectories.h" int FPSCap=5; NPCTrajectory::NPCTrajectory(int npoints, int newSpeed) { numOfPoints=npoints; this-&gt;speed=newSpeed; points = new int * [npoints]; for (int i=0; i&lt;npoints; i++) points[npoints] = new int[2]; state = 0; maxOffset=speed/FPSCap; } void NPCTrajectory::AddCoordinates(int ** coordinates) { for(int i=0;i&lt;this-&gt;numOfPoints; i++) { points[i][0]=coordinates[i][0]; points[i][1]=coordinates[i][1]; } } void NPCTrajectory::HandleEvents(double (&amp;currentCoordinates)[2]) { if(state+1==numOfPoints) return; if(Distance(currentCoordinates[0], currentCoordinates[1], (double)points[state+1][0], (double)points[state+1][1])&lt;maxOffset) state++; double ratio = maxOffset/Distance(currentCoordinates[0], currentCoordinates[1], (double)points[state+1][0], (double)points[state+1][1]); currentCoordinates[0]+=(points[state+1][0]-currentCoordinates[0])*ratio; currentCoordinates[1]+=(points[state+1][1]-currentCoordinates[1])*ratio; } </code></pre> <p>Please note that removing command traj.AddCoordinates(coords) will make the problem disappear. Am I passing the array correctly to the function? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload