opengl - Getting wide range of type errors in C++ -
this question has answer here:
here important part of program getting wide range of type errors. trying make array of objects use struct hold data.
any help?
static object objectarray[number_of_objects]; class object { public: float x; float y; float z; int id; }; void createobjects(){ (int = 0; < 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(){ (int = 0; < 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(){ (int = 0; < 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(); } }
here error:
error 2 error c4430: missing type specifier - int assumed. note: c++ 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 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
here complete source code:
#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(){ (int = 0; < 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(){ (int = 0; < 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(){ (int = 0; < 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++'s compiler performs single-pass (unlike c#s , java's multiple passes), means compiler must aware of type before it's used.
here:
foo bar[n]; class foo { };
...doesn't work, compiler sees array definition bar[n]
doesn't know foo
is.
you have 2 options:
forward-declare
foo
without defining it, compiler knowsfoo
exists (and knows can find definition later) - note difference between "definition" , "declaration"class foo; // forward-declaration foo bar[n]; // usage class foo { }; // definition
move declarations around type
foo
defined before use it:class foo {}; // definition foo bar[n]; // usage
also, general c++ tips:
i recommend giving types (class
es, struct
s, union
s, typedef
s, etc) names in titlecase
, fields, locals, parameters , globals should (in opinion) given camelcase
make obvious if code involves instance or static members , avoid ambiguities.
static object objectarray[number_of_objects];
becomes
static object objectarray[ number_of_objects ];
(also, avoid using name object
many libraries use name. consider entity
or prefix short identifier project, or move within namespace.)
the expression object obj;
in code create instance of object
in stack , perform copy when moves array. unnecessary. if change code run faster no copying involved:
(int = 0; < number_of_objects; i++){ objectarray[i].x = rand() % 13 - 6; objectarray[i].y = 0; objectarray[i].z = rand() % 13 - 6; objectarray[i].id = i; }
as microptimization, consider caching offset pointer:
object* arrayitem = objectarray[i]; arrayitem->x = rand() % 13 - 6; arrayitem->y = 0; arrayitem->z = rand() % 13 - 6;
...but splitting hairs , it's possible optimizing compiler you.
Comments
Post a Comment