Hello everyone,
I am new in the C++ world of programming and I am getting desperate with the following code. Basically, I am trying to get a variable defined as private in my class from a public function. The variable is actually a set of integers. My code is divided into three different files:
#include <iostream>
#include <cstdio>
#include "data.h"
usingnamespace std;
int main (int argc, char **argv){
Data d;
for (int i=0; i<10; i++)
d.add_datum(i);
printf("size %d ",(int)d.get_data().size());
for(set<int>::iterator it=d.get_data().begin(); it!=d.get_data().end(); it++)
printf("%d ",*it);
printf("\n");
return 0;
}
The object set is supposed to contain integers from 0 to 9, but when I try to iterate through its elements by making use of the function "get_data()" I can not get the whole set, but only some of the elements, even though it seems to be correct, since d.get_data().size() is 10. The execution gives me:
./main
size 10 3 9
Any ideas? I guess I must be doing something really wrong, but I cannot figure out what... Many thanks in advance.
each time you call get_data(void ) you get a copy of data, hence d.get_data().begin() and d.get_data().end() are from different object. It's not defined what happens.