迭代使用指针和for循环的函数

时间:2015-07-27 09:44:52

标签: c++ pointers

int main()
{
    using namespace std;
    int i=3;
    radius *Zone1;
    Zone1=ZoneSorting(6088, i);
    zone *ZONE;
    ZONE=ZoneGeneration(rN, rA, rB, rC, 6088);
    CentralLocalHeliostatDetector(i, ZONE[i], Zone1, 10);
    getch();
}

这很好用!现在我想重复一遍i = 0到i = 32 所以我试过这个。但它会导致崩溃...

int main()
{
    using namespace std;

    radius *Zone1;
    zone *ZONE;
    ZONE=ZoneGeneration(rN, rA, rB, rC, 6088);

    for(int i=1; i<32; i++)
    {
        Zone1=ZoneSorting(6088, i);
        CentralLocalHeliostatDetector(i, ZONE[i], Zone1, 10);
        cout<<"===================="<<endl;
        delete[] Zone1;
    }
    getch();
}

2 个答案:

答案 0 :(得分:0)

你为什么打电话给删除?这用于删除使用new运算符分配的堆对象。如果您希望每次循环都使用名为Zone1的新ZoneSorting对象,则可以执行

for(int i=1; i<32; i++)
{
   ZoneSorting Zone1(6088, i);
   CentralLocalHeliostatDetector(i, ZONE[i], Zone1, 10);
   cout<<"===================="<<endl;
}

当然,正如其他响应者所说,我不确定我是否真正了解您的代码应该做什么。

答案 1 :(得分:-1)

尝试从for循环边界中删除delete[] Zone1;行。 或者如果您需要,请尝试将其替换为delete[i] Zone1;