Re: error while using the function size ()
On 4/28/2014 7:09 AM, Milica Milojevic wrote:
Hi,
I have created a list called measuresQueue (list<pssMeasure *> measuresQueue; ) which is consisted of ten elements. Then I used measuresQueue.size() function. When I did the printf of the previous function it prints me the number four not ten. Can someone help me with this problem.
Here is my code:
In for loop I have entered my elements of the list:
for (filtertime_t=0; filtertime_t<=1.8; filtertime_t=filtertime_t+0.2) {
pssMeasure *mes = measureReceived(filtertime_t,y,mes_vect,mes_cov);
y++;
process_th->critical_section->writeMeasures(mes);
}
Here it is the measureReceived function description:
pssMeasure * measureReceived(double timeStamp,int j,double array[3][10], double array_cov[3]) {
pssMeasure *mes = new pssMeasure();
mes->track_id =7;
mes->timeStamp =timeStamp;
for (int i=0; i<3; i++)
{
mes->z_t[i]=array[i][j];
}
for(int i = 0; i < 3; i++) {
for(int k = 0; k < 3; k++)
mes->cov_z[i][k] = 0.0;
mes->cov_z[i][i] = array_cov[i];
}
return mes;
}
After creating mes I store them using the function writeMeasures:
void CPSSCriticalSection::writeMeasures(pssMeasure* pss_mes) {
measuresQueue.push_back(pss_mes);}
In order to check that my all elements are stored in the measuresQueue list I have used this after the measuresQueue.push_back(pss_mes);
cout << "hello1" << endl
printf("%f %f %f %f \n", pss_mes->timeStamp, pss_mes->z_t[0], pss_mes->z_t[1], pss_mes->z_t[2]);
Everything was fine until I wanted to see the size of my list measuresQueue with the: cout << "measuresQueue. size: " << measuresQueue.size() << '\n';
I always get the number 4 instead of ten. I don't know what is the reason?
The reason is likely some kind of logic mistake in some other part of
your program.
Can you simplify your program and exclude all parts that you don't show
here and try a very simple test:
#include <list>
#include <iostream>
struct pssMeasure {}; // yes, empty
struct CPS {
std::list<pssMeasure*> measuresQueue; // yes, only this
void writeMeasures(pssMeasure* m) {
measuresQueue.push_back(m);
}
void dump(std::ostream& os) {
os << "measuresQueue.size : " << measuresQueue.size();
}
};
int main() {
CPS cps;
for (double t = 0; t <= 1.8 ; t+=0.2)
{
pssMeasure *pm = new pssMeasure;
cps.writeMeasures(pm);
}
cps.dump(std::cout);
}
? What output do you get? I get '10'.
V
--
I do not respond to top-posted replies, please don't ask