<分区>
这是我的程序的重要部分,我遇到了各种各样的类型错误。我正在尝试制作一个对象数组,以用作类似于 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 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-HW4
Error 4 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 46 1 CS459-HW4
Error 5 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 47 1 CS459-HW4
Error 7 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 48 1 CS459-HW4
Error 9 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 49 1 CS459-HW4
Error 11 error C2065: 'obj' : undeclared identifier c:\users\amir-acer\documents\visual studio 2013\projects\cs459-hw4\cs459-hw4\source.cpp 50 1 CS459-HW4
Error 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.14159265
static 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;
}