简体   繁体   中英

C++ - values not getting set correctly in for loop

I am trying to set the values for the 2nd and 3rd corners on three objects of class triangle. If done explicitly, this looks like this:

triangle_mesh[0].set_vector_point2(vector_anchors[1]);
triangle_mesh[1].set_vector_point3(vector_anchors[1]);
triangle_mesh[1].set_vector_point2(vector_anchors[2]);
triangle_mesh[2].set_vector_point3(vector_anchors[2]);
triangle_mesh[2].set_vector_point2(vector_anchors[3]);
triangle_mesh[0].set_vector_point3(vector_anchors[3]);

and this works ! Printing these triangles, I get (nb the first corner has already been set - this is not an issue):

triangle0 is ( 0, 0, -1) (1, 0, 0) (-0.5, -0.866025, 0)
triangle1 is ( 0, 0, -1) (-0.5, 0.866025, 0) (1, 0, 0)
triangle2 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, 0.866025, 0)

Firstly, though, this is ugly, and secondly it must generalise to cases where I have more than three triangles to set up. My code for this is:

for (int longitude = 0; longitude < num_longitudes; longitude++){
  SurfaceVector current_anchor = vector_anchors[1 + longitude];
    triangle_mesh[longitude].set_vector_point2(current_anchor);
    triangle_mesh[(longitude + 1) % num_longitudes].set_vector_point3(current_anchor);
}

*nb num_longitudes is 3*

I've checked everything I can think of, but now when I print out my triangles I get:

triangle0 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
triangle1 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
triangle2 is ( 0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)

Does anyone have any idea what could be going wrong?!

EDIT

The vector_point variables on the triangle are pointers, and are set like:

void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }

There lies your problem:

void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }

You are pointing to a temporary ( vector_point ceases to exist once the function call completes). Change it so you copy SurfaceVector properly.

I'd change:

void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }

to

void set_vector_point1(SurfaceVector& vector_point) { vector_point1 = &vector_point; }

Or something like that.

In the current version vector_point will be a copy of whatever you pass, and will no longer exist after, you're storing a pointer to an object that no longer exists.

In the second one, vector_point is a reference to a longer lived object external to the function. Storing a pointer to that is fine as the object will still be there when you use the pointer.

The trick is to make sure the object lives longer than all the pointers to it.

ADDITION:

Thanks @Nim in the below comment:

Also in the for loop where the line is:

SurfaceVector current_anchor = vector_anchors[1 + longitude];

That should probable be a reference too .. at the moment it too is a copy. That way you'll be editing the actual objects in the array, rather than playing around with copies and throwing them away. So I would change that line to:

SurfaceVector& current_anchor = vector_anchors[1 + longitude];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM