自从我上次使用向量和其他结构(大多数在大学)编程以来,已经有好几年了,现在我遇到了一个问题,我无法确定这个知识差距。< / p>
所以,这里是代码的一部分,我认为它有问题:
if (pos == -1) {
cout<< "New Neighbor\n";
neighborRecord newRec; //declaring newRec as type struct neighborRecord
newRec.id = nodeID; //receiving values
newRec.x = x_ngb;
newRec.y = y_ngb;
newRec.z = z_ngb;
newRec.x_dst = x_dst; //L
newRec.y_dst = y_dst;
newRec.z_dst = z_dst;
newRec.ts = simTime().dbl();
newRec.timesRx = 1;
newRec.ic = IC;
cout<< "NEW REC IC" << newRec.ic<<"\n"; //until here i checked and it's all ok with variables
neighborTable.push_back(newRec);
但是当我创建一个循环来读取矢量时,我得到了错误的值。 这是循环:
for (int i = 0; i < neighborTable.size(); i++){
trace()<< "ID: "<< neighborTable[i].id <<"\n";
trace()<< "X : "<< neighborTable[i].x<<"\n";
trace()<< "Y : "<< neighborTable[i].y<<"\n";
trace()<< "Z : "<< neighborTable[i].z<<"\n";
trace()<< "XDST: "<< neighborTable[i].x_dst<<"\n";
trace()<< "YDST: "<< neighborTable[i].y_dst<<"\n";
trace()<< "ZDST: "<< neighborTable[i].z_dst<<"\n";
trace()<< "TS: "<< neighborTable[i].ts <<"\n";
trace()<< "TIMESRX: "<< neighborTable[i].timesRx<<"\n";
trace()<< "IC: "<< neighborTable[i].ic<<"\n";
}
结果之一:
ID:30
X:26.4458
Y:38.1514
Z:0
XDST:5.43472e-323
YDST:51.8989
ZDST:50.9054
TS:1.13635e-322
TIMESRX:20615843
IC:0
我不知道访问该向量时是否有什么,我不知道如何使其工作。有趣的是,我用较少的变量进行了测试,并且工作正常。当我向结构中添加更多变量时,我才刚刚看到这个错误。