-1

我对 c++ 相当陌生,不确定我是否正确解释了这一点。

我有一段代码(一个函数)在运行整个程序时没有结束。

该函数的目的是首先生成一个从 0 到 1 的随机数。接下来,如果该随机数大于 0.6,则进入另一个函数。该功能 virus_death 完美运行。如果随机数小于0.6,那么它会使病毒点推回到一个叫做reached的向量中。它还会将该点写入文件。然而,这种说法从未停止。

void replication(vector<Virus>& virus, int a, int b, double& times, int& iv, 
                 ofstream& data_file, int p, vector<Direction>& direction_v, 
                 vector<Virus>&replicate, int step)
{
    point R,P;

    double replicates  = ((double) rand() / (RAND_MAX));
    //how long the virus takes to reach the necleus

    if( replicates>0.6)
    {
        cout<<"No Replication "<<endl;
        virus_death(times, iv, virus,a,b,data_file,p,direction_v, step);
    }
    else if( replicates<=0.6)
    {
        cout<<"Replication"<<endl;

        ofstream tom;
        tom.open("Timing_nuc.dat");

        cout<<"Points"<<endl;
        R.x = virus[p].getx();
        R.y = virus[p].gety();
        R.t = virus[p].gett();

        cout<<"Write to File"<<endl;
        tom<<p+1<<"\t"<<R.x<<"\t"<<R.y<<"\t"<<R.t<<"\n";

        cout<<"New Points"<<endl;
        P.x = a;
        P.y = b;
        P.t = 0;
        cout<<P.x<<"\t"<<P.y<<"\t"<<P.t<<"\n";


        replicate.push_back(P);
        int aspa = replicate.size();
        cout<<"Replication Size:"<<aspa<<endl;
    }           
}

有人可以帮我确定为什么一旦将点推入向量中,它就不会离开函数。

调用这个函数的代码是:

void location_of_virus(vector<Virus>&virus,double& times, int& iv,
                        ofstream& data_file, vector<Direction>& direction_v,
                        vector<Virus>&virus_engulf, circle&cell_c, 
                        circle&nuclei_c, vector<Virus>&replicate, int step)
{
    int a;
    int b;

    //int nc,cc,cmc,nci;
    float dis_c, dis_n;

    int virus_size= virus.size();
    //type = "virus";
    for(int i =0; i<virus_size; i++)
    {
        a = virus[i].getx();
        b = virus[i].gety();

        //to check where the virus is in location to the environment
        dis_c = cell_c.distance(a,b);
        dis_n = nuclei_c.distance(a,b);

        if(dis_n < 5)
        {
            location =" Inside Nucleus";
            cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl;
            data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n";
            replication(virus,a,b,times,iv,data_file,i,direction_v,replicate,step);
        }   
        else if(dis_n ==5)
        {
            location = "Nucleus";
            cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl;
            data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n";
            nucleus(virus,a,b,times,iv,data_file,i,direction_v,step);

        }
        else if(dis_c <25 && dis_n>5)
        {
            location = "Cytoplasm";
            cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl;
            data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n";
            cytoplasm(virus,a,b,times,iv,data_file,p,direction_v,step);
        }
        else if(dis_c ==25)
        {
            location = "Cell Membrane";
            cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl;
            data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n";
            endocytosis(virus,a,b,times,iv, data_file,i,direction_v,virus_engulf,cell_c,nuclei_c);
        }
        else if (((a ==0 || a == 50 || a>0 || a<50)&& (b ==0 || b == 50 || b>0 || b<50)) && dis_c>15)
        {
            location ="Extracellular Matrix";
            cout<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<endl;
            data_file<<i+1<<"\t"<<virus[i]<<"\t"<<direction_v[i]<<"\t"<<location<<"\n";
        }
        else
        {
            location ="Left the System";
            cout<<i+1<<"\t"<<virus[i]<<"\t"<<location<<endl;
            data_file<<i+1<<"\t"<<virus[i]<<"\t"<<location<<"\n";

        }
    }
}
4

1 回答 1

1

你确定你在某处没有无限递归吗?例如,也许会再次virus_death打电话location_of_virus左右?

此外,您并没有真正检查是否virus[p]有效。p如果在其范围内,可能会添加一个小的安全检查。

如果你不知何故弄乱了你的堆栈,各种奇怪的事情都可能发生——还有一些无休止的循环。

于 2012-12-11T03:15:29.050 回答