-1

我正在测试我的一个课程,但由于某种原因,我似乎无法将二维数组中的整数转换为加倍。这是我的(非常简化的)代码:在 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) 将使问题消失。我是否将数组正确传递给函数?

4

1 回答 1

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 访问 points[i][0](假设您在 NPCTrajectory 中给出 npoints>0)时,您会在 AddCoordinates 函数中遇到错误(分段错误)。

于 2013-11-11T00:22:18.483 回答