存储到数组的问题

时间:2013-11-10 23:37:45

标签: c++ arrays segmentation-fault

我正在测试我的一个类,但由于某种原因,我似乎无法将一个2d数组中的intiger转换为double。这是我的(非常简化的)代码: 在main.cpp中

    #include<iostream>
    #include<conio.h>
    #include<string>
    #include "trajectories.h"
    int main()
    {
        std::string response;
        int numOfCoords;
        int speed;
        int ** coords;

        std::cout<<"enter the number of coordinates: ";
        std::cin>>numOfCoords;
        std::cout<<"enter speed: ";
        std::cin>>speed;

        coords=new int *[numOfCoords];
        for (int i=0; i<numOfCoords; i++)
        coords[i] = new int[2];

        for(int i=0; i<numOfCoords*2; i++)
        {
                if(i%2==0)
                std::cout<<"enter point "<<i/2<<".x : ";
                else
                std::cout<<"enter point "<<i/2<<".y : ";
                std::cin>>coords[i/2][i%2];
        }

        NPCTrajectory traj(numOfCoords, speed);
        traj.AddCoordinates(coords);

        std::cout<<coords[0][0]<<", "<<coords[0][1]<<std::endl;
        getch();

        double currentCoords[2];
        currentCoords[0]=double(coords[0][0]);
        currentCoords[1]=double(coords[0][1]);

for(;;)
    {

           traj.HandleEvents(currentCoords);
           std::cout<<"current coordinates : ("<<currentCoords[0]<<", "<<currentCoords[1]<<")"<<std::endl;
           std::cout<<"do you wish to continue? ";
           getch();
    }
    }

Trajectories.h只包含类声明,所以我认为它是无关紧要的。这是我的轨迹.cpp

    #include "trajectories.h"

        int FPSCap=5;

        NPCTrajectory::NPCTrajectory(int npoints, int newSpeed)
        {
              numOfPoints=npoints;
              this->speed=newSpeed;
              points = new int * [npoints];
              for (int i=0; i<npoints; i++)
              points[npoints] = new int[2];
              state = 0;
              maxOffset=speed/FPSCap;
        }

        void NPCTrajectory::AddCoordinates(int ** coordinates)
        {
             for(int i=0;i<this->numOfPoints; i++)
             {
                     points[i][0]=coordinates[i][0];
                     points[i][1]=coordinates[i][1];
             }
        }
void NPCTrajectory::HandleEvents(double (&currentCoordinates)[2])
{
     if(state+1==numOfPoints) return;
     if(Distance(currentCoordinates[0], currentCoordinates[1], (double)points[state+1][0], (double)points[state+1][1])<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;
}

请注意删除命令traj.AddCoordinates(coords)会使问题消失。我是否正确地将数组传递给函数?

1 个答案:

答案 0 :(得分:1)

问题出在您的构造函数NPCTrajectory中。用循环变量i替换npoints。以下代码:

for (int i=0; i<npoints; i++)
          points[npoints] = new int[2];

应该是:

for (int i=0; i<npoints; i++)
          points[i] = new int[2];

由于这种不正确的分配,当你尝试访问i = 0的点[i] [0]时,你会在AddCoordinates函数中得到错误(分段错误)(假设你在NPCTrajectory中给npoints> 0)。