C++ - for循环中没有正确设置值

tis*_*was 0 c++ loops setvalue

我试图在三角形的三个对象上设置第二和第三角的值.如果明确地完成,这看起来像这样:

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]);
Run Code Online (Sandbox Code Playgroud)

这很有效!打印这些三角形,我得到(nb第一个角已经设置 - 这不是问题):

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)
Run Code Online (Sandbox Code Playgroud)

首先,这是丑陋的,其次它必须推广到我有三个以上三角形设置的情况.我的代码是:

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);
}
Run Code Online (Sandbox Code Playgroud)

*nb num_longitudes是3*

我已经检查了我能想到的一切,但现在当我打印出三角形时,我得到:

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)
Run Code Online (Sandbox Code Playgroud)

有谁知道会出现什么问题?!

编辑

三角形上的vector_point变量是指针,设置如下:

void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }
Run Code Online (Sandbox Code Playgroud)

Nim*_*Nim 5

你的问题存在:

void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }
Run Code Online (Sandbox Code Playgroud)

您指向临时(vector_point一旦函数调用完成就不再存在).更改它,以便您SurfaceVector正确复制.