int size = objects_op->size();
for (auto &at : objects_op->object) {
EmObject& at = objects_op->objects().at(i);
if (at.sub_type != ObjectType::VISION_PLD) {
continue;
}
for (size_t i = 0; i < 4; i++) {
std::cout << " c: " << at.object.polygon.pts.at(i).transpose()
std::endl;
}
std::cout << BLUE << "after:" << &(at.object.polygon.pts) << RESET
std::endl;
}
the above code is right result:
0 c: -3.38838 -4.42124
1 c: -5.64087 -4.45888
2 c: -5.56526 -9.241
3 c: -3.32031 -9.20348
after:0xf5d3760
0 c: -7.13594 3.63844
1 c: -9.28928 3.66867
2 c: -9.26098 8.2844
3 c: -7.10295 8.25414
after:0xf5d3880
but for the below
int size = objects_op->size();
for (size_t i=0; i< size; i++) {
EmObject& at = objects_op->objects().at(i);
if (at.sub_type != ObjectType::VISION_PLD) {
continue;
}
for (size_t i = 0; i < 4; i++) {
std::cout << " c: " << at.object.polygon.pts.at(i).transpose()
std::endl;
}
std::cout << BLUE << "after:" << &(at.object.polygon.pts) << RESET
std::endl;
}
the above code is right result:
0 c: -14.9e-18 1.9e+18
1 c: -5.9e-18 -4.4e+17
2 c: -5.56526 -9.241
3 c: -3.32031 -9.20348
after:0xf5d3720
0 c: -2.9e-18 1.2e+18
1 c: -3.9e-18 -4.3e+17
2 c: -9.26098 8.2844
3 c: -7.10295 8.25414
after:0xf5d3870
I don not understand how it happen, please help..
The issue you’re encountering is due to the scope of the variable i
in your second code snippet. In the inner loop, you’re redeclaring i
for the loop variable, which shadows the i
used in the outer loop for indexing the objects. This is why you’re seeing unexpected results.
You can change the variable in the inner loop from i
to j
. This way, the inner loop variable doesn’t interfere with the outer loop variable.