我对c ++很新,我不确定我是否正确解释了这一点。
我有一段代码(一个函数)在运行整个程序时没有结束。
该函数的目的是首先生成一个从0到1的随机数。然后,如果该随机数大于0.6,它将进入另一个函数。那个函数virus_death工作得很好。如果随机数小于0.6,那么它将使病毒点推回到一个名为达到的向量中。它还会将该点写入文件。但是,该声明永远不会停止。
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";
}
}
}
答案 0 :(得分:1)
你确定在某个地方没有无限递归吗?例如。也许virus_death
再次调用location_of_virus
?
此外,您并未真正检查virus[p]
是否有效。如果p
在其范围内,可以添加一个小的安全检查。
如果你出于某种原因某种原因弄乱你的堆栈,可能会发生各种奇怪的事情 - 也就是一些无限循环。