This question already has answers here: C++ how to declare a vector of objects as a member of a class (3个答案) 5年前关闭。 这是我程序的重要部分,我遇到了很多类型错误。我正在尝试制作一个对象数组,以用作诸如Struct之类的东西来仅保存数据。有什么帮助吗?static Object ObjectArray[NUMBER_OF_OBJECTS];class Object{public: float X; float Y; float Z; int id;};void createObjects(){ for (int i = 0; i < NUMBER_OF_OBJECTS; i++){ Object obj; obj.X = rand() % 13 - 6; obj.Y = 0; obj.Z = rand() % 13 - 6; obj.id = i; ObjectArray[i] = obj; }}void randomizeObjects(){ for (int i = 0; i < NUMBER_OF_OBJECTS; i++){ ObjectArray[i].X += rand() % 3 - 1; ObjectArray[i].Z += rand() % 3 - 1; if (ObjectArray[i].X > 6) ObjectArray[i].X--; if (ObjectArray[i].X < -6) ObjectArray[i].X++; if (ObjectArray[i].Z > 6) ObjectArray[i].Z--; if (ObjectArray[i].Z < -6) ObjectArray[i].Z++; }}void drawObjects(){ for (int i = 0; i < NUMBER_OF_OBJECTS; i++){ ObjectArray[i].X += rand() % 3 - 1; ObjectArray[i].Z += rand() % 3 - 1; glPushMatrix(); glTranslatef(ObjectArray[i].X, ObjectArray[i].Y, ObjectArray[i].Z); glutSolidSphere(0.5, 10, 10); glPopMatrix(); }}这是错误:Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 32 1 CS459-HW4Error 29 error C2660: 'glTranslatef' : function does not take 1 arguments c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 77 1 CS459-HW4Error 10 error C2228: left of '.Z' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 49 1 CS459-HW4Error 15 error C2228: left of '.Z' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 58 1 CS459-HW4Error 20 error C2228: left of '.Z' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 64 1 CS459-HW4Error 21 error C2228: left of '.Z' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 65 1 CS459-HW4Error 22 error C2228: left of '.Z' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 66 1 CS459-HW4Error 23 error C2228: left of '.Z' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 67 1 CS459-HW4Error 25 error C2228: left of '.Z' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 74 1 CS459-HW4Error 28 error C2228: left of '.Z' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 77 1 CS459-HW4Error 8 error C2228: left of '.Y' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 48 1 CS459-HW4Error 27 error C2228: left of '.Y' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 77 1 CS459-HW4Error 6 error C2228: left of '.X' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 47 1 CS459-HW4Error 14 error C2228: left of '.X' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 57 1 CS459-HW4Error 16 error C2228: left of '.X' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 60 1 CS459-HW4Error 17 error C2228: left of '.X' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 61 1 CS459-HW4Error 18 error C2228: left of '.X' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 62 1 CS459-HW4Error 19 error C2228: left of '.X' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 63 1 CS459-HW4Error 24 error C2228: left of '.X' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 73 1 CS459-HW4Error 26 error C2228: left of '.X' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 77 1 CS459-HW4Error 12 error C2228: left of '.id' must have class/struct/union c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 50 1 CS459-HW4Error 1 error C2146: syntax error : missing ';' before identifier 'ObjectArray' c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 32 1 CS459-HW4Error 3 error C2146: syntax error : missing ';' before identifier 'obj' c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 46 1 CS459-HW4Error 4 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 46 1 CS459-HW4Error 5 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 47 1 CS459-HW4Error 7 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 48 1 CS459-HW4Error 9 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 49 1 CS459-HW4Error 11 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 50 1 CS459-HW4Error 13 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 51 1 CS459-HW4这是完整的源代码:#include <GL/glut.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#define NUMBER_OF_OBJECTS 10#define PI 3.14159265static int step = 0;static float alpha = 0.0;static float beta = PI / 6.0;static GLdouble cpos[3];static GLfloat lpos[] = { -3.5, 3.5, 3.5, 1.0 };static GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };static GLfloat gray[] = { 0.5, 0.5, 0.5, 1.0 };static GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };static GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };static GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };static GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };static GLfloat darkcyan[] = { 0.0, 0.4, 0.4, 1.0 };static GLfloat gold[] = { 255.0 / 255.0, 215.0 / 255.0, 0 / 255.0 };static GLfloat silver[] = { 204.0 / 255.0, 204.0 / 255.0, 204.0 / 255.0 };static bool trianglesExist = false;static float stepBefore = step;static float zoom = 10.0;static bool lightSource = true;static Object ObjectArray[NUMBER_OF_OBJECTS];class Object{public: float X; float Y; float Z; int id;};void createObjects(){ for (int i = 0; i < NUMBER_OF_OBJECTS; i++){ Object obj; obj.X = rand() % 13 - 6; obj.Y = 0; obj.Z = rand() % 13 - 6; obj.id = i; ObjectArray[i] = obj; }}void randomizeObjects(){ for (int i = 0; i < NUMBER_OF_OBJECTS; i++){ ObjectArray[i].X += rand() % 3 - 1; ObjectArray[i].Z += rand() % 3 - 1; if (ObjectArray[i].X > 6) ObjectArray[i].X--; if (ObjectArray[i].X < -6) ObjectArray[i].X++; if (ObjectArray[i].Z > 6) ObjectArray[i].Z--; if (ObjectArray[i].Z < -6) ObjectArray[i].Z++; }}void drawObjects(){ for (int i = 0; i < NUMBER_OF_OBJECTS; i++){ ObjectArray[i].X += rand() % 3 - 1; ObjectArray[i].Z += rand() % 3 - 1; glPushMatrix(); glTranslatef(ObjectArray[i].X, ObjectArray[i].Y, ObjectArray[i].Z); glutSolidSphere(0.5, 10, 10); glPopMatrix(); }}void writemessage(){}void init(void){ writemessage(); glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glEnable(GL_LIGHT0);}void reshape(int w, int h){ glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(75.0, (GLfloat)w / (GLfloat)h, 1.0, 25.0); glMatrixMode(GL_MODELVIEW);}void display(void){ static int i; static double angle1, angle2, angle3, angle4, Y1, Z1, Y2, Z2, X3, Y3, X4, Y4; static double angle5, angle6, angle7, angle8, X5, Y5, X6, Y6, X7, Y7, X8, Y8; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); cpos[0] = zoom * cos(beta) * sin(alpha); cpos[1] = zoom * sin(beta); cpos[2] = zoom * cos(beta) * cos(alpha); gluLookAt(cpos[0], cpos[1], cpos[2], 0.0, 1.0, 0.0, 0.0, 1.0, 0.0); if (lightSource == true){ glLightfv(GL_LIGHT0, GL_POSITION, lpos); glMaterialfv(GL_FRONT, GL_EMISSION, white); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 128); glPushMatrix(); glTranslatef(lpos[0], lpos[1], lpos[2]); glutSolidSphere(0.1, 10, 8); glPopMatrix(); } glMaterialfv(GL_FRONT, GL_EMISSION, black); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue); glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, blue); glNormal3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON); glVertex3f(7.0, -0.001, -7.0); glVertex3f(-7.0, -0.001, -7.0); glVertex3f(-7.0, -0.001, 7.0); glVertex3f(7.0, -0.001, 7.0); glEnd(); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green); glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, green); glMaterialfv(GL_BACK, GL_SPECULAR, green); drawObjects(); glFlush(); glutSwapBuffers();}void keyboard(unsigned char key, int x, int y){ static int polygonmode[2]; switch (key) { case 27: exit(0); break; case 'w': glGetIntegerv(GL_POLYGON_MODE, polygonmode); if (polygonmode[0] == GL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glutPostRedisplay(); break; case 'x': if (lightSource == true) lpos[0] = lpos[0] + 0.2; glutPostRedisplay(); break; case 'X': if (lightSource == true) lpos[0] = lpos[0] - 0.2; glutPostRedisplay(); break; case 'y': if (lightSource == true) lpos[1] = lpos[1] + 0.2; glutPostRedisplay(); break; case 'Y': if (lightSource == true) lpos[1] = lpos[1] - 0.2; glutPostRedisplay(); break; case 'z': if (lightSource == true) lpos[2] = lpos[2] + 0.2; glutPostRedisplay(); break; case 'Z': if (lightSource == true) lpos[2] = lpos[2] - 0.2; glutPostRedisplay(); break; case '+': if (zoom != 1.5)zoom = zoom - 0.5; glutPostRedisplay(); break; case '-': if (zoom != 15)zoom = zoom + 0.5; glutPostRedisplay(); break; case '0': if (lightSource == true){ glDisable(GL_LIGHT0); lightSource = false; } else{ glEnable(GL_LIGHT0); lightSource = true; } glutPostRedisplay(); break; default: break; }}void specialkey(GLint key, int x, int y){ switch (key) { case GLUT_KEY_RIGHT: alpha = alpha + PI / 180; if (alpha > 2 * PI) alpha = alpha - 2 * PI; glutPostRedisplay(); break; case GLUT_KEY_LEFT: alpha = alpha - PI / 180; if (alpha < 0) alpha = alpha + 2 * PI; glutPostRedisplay(); break; case GLUT_KEY_UP: if (beta < 0.45*PI) beta = beta + PI / 180; glutPostRedisplay(); break; case GLUT_KEY_DOWN: if (beta > -0.05*PI) beta = beta - PI / 180; glutPostRedisplay(); break; default: break; }}int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); glutInitWindowSize(800, 800); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(specialkey); glutMainLoop(); return 0;} 最佳答案 C ++的编译器执行一次遍历(与C#和Java的多次遍历不同),这意味着编译器必须在使用某种类型之前就知道其类型。这里:Foo bar[N];class Foo { };...不起作用,编译器会看到数组定义bar[N],但不知道Foo是什么。您有两种选择:转发声明Foo而不定义它,因此编译器知道Foo存在(并且知道以后可以找到该定义)-注意“ definition”和“ declaration”之间的区别class Foo; // forward-declarationFoo bar[N]; // usageclass Foo { }; // definition移动声明,以便在使用Foo类型之前对其进行定义:class Foo {}; // definitionFoo bar[N]; // usage另外,一些通用的C ++技巧:我建议仅在class中给出类型(struct es,union s,typedef s,TitleCase s等)的名称,字段,局部变量,参数和全局变量(在我看来)可以使您的代码包含实例或静态成员而变得显而易见,并避免歧义。static Object ObjectArray[NUMBER_OF_OBJECTS];变成static Object objectArray[ NUMBER_OF_OBJECTS ];(此外,我会避免使用名称camelCase,因为许多库都使用该名称。考虑使用Object或为项目添加一个短标识符作为前缀,或将其移至名称空间中。)代码中的表达式Entity将在堆栈中创建Object obj;的实例,并在将其移入数组时执行复制。这是不必要的。如果将代码更改为此,它将运行得更快,因为不会涉及复制: for (int i = 0; i < NUMBER_OF_OBJECTS; i++){ objectArray[i].X = rand() % 13 - 6; objectArray[i].Y = 0; objectArray[i].Z = rand() % 13 - 6; objectArray[i].id = i;}作为微优化,请考虑缓存偏移量指针:Object* arrayItem = objectArray[i];arrayItem->X = rand() % 13 - 6;arrayItem->Y = 0;arrayItem->Z = rand() % 13 - 6;...但这很麻烦,优化的编译器可能会为您完成此操作。 10-04 10:28