我在测试程序中定义一个存储三维点的结构体,并定义该结构体的vector,当我在向vector插入元素时,编译一直提示错误:
代码片段如下:
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <GL/glut.h>
#include <GL/glu.h> #include <GL/gl.h> #include <vector> using namespace std; #define HEIGHT 480 #define WIDTH 640 typedef struct Point3D { double objx; double objy; double objz; } POINT_3D, *PPOINT_3D; double objx = 0, objy = 0, objz = 0; double objnx = 0, objny = 0, objnz = 0; double objfx = 0, objfy = 0, objfz = 0; double angle = 0.0; extern vector<POINT_3D> vec3dPoint; vector<POINT_3D>::iterator it; POINT_3D objxyz; objxyz.objx = objx; objxyz.objy = objy; objxyz.objz = objz; vec3dPoint.push_back(objxyz); |
问题在于我将代码操作放在了全局区域中,正确的做法应该放在函数中:
C++ Code
1
2 3 4 5 6 7 8 |
void initVector()
{ POINT_3D objxyz; objxyz.objx = objx; objxyz.objy = objy; objxyz.objz = objz; vec3dPoint.push_back(objxyz); } |
好久没有敲过代码了,有点生疏了~ 记录警示一下!