I have a point3 struct that takes 3 floats x, y, z (3-D space coordinates).
I’m trying to write a function that translates the x, y, z values of each element in a list of points.
I’m new to C++ so I don’t think I wrote the iterator correctly, can anyone help clarify how to iterate down a list and modify components of each element in the list?
I looked at this post C++ How to loop through a list of structs and access their properties and thought this answered my problem, but the solution didn’t work.
Here is my code:
//Translates the face by dx, dy, dz coordinates
list<point3> translateFace(list<point3> lop, float dx, float dy, float dz)
{
list<point3>::iterator iter;
for (iter = lop.begin() ; iter != lop.end(); iter++){
iter->x - dx;
iter->y - dy;
iter->z - dz;
}
return lop;
}
I also tried the solution suggested in the link above, still didn’t work. Should return lop be inside or outside the for loop? Should I be returning something other than lop?
for (iter = lop.begin() ; iter != lop.end(); iter++){
(*iter).x - dx;
(*iter).y - dy;
(*iter).z - dz;
}
return lop;
It appears to me that you’re not actually storing the results of your calculation:
Try this:
I would hope most compilers would throw a warning about an unused calculation like this — turn up the warning level on your compiler if you can.