这是我的程序的重要部分,我遇到了各种各样的类型错误。我试图将一个对象数组用作像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;
}
答案 0 :(得分:2)
C ++编译器执行单次传递(与C#和Java不同,它执行多次传递),这意味着编译器必须在它之前知道一个类型&#39;使用过。
下面:
Foo bar[N];
class Foo { };
...不起作用,编译器会看到数组定义bar[N]
,但不知道Foo
是什么。
您有两种选择:
转发声明Foo
而不定义它,因此编译器知道Foo
存在(并且它知道它可以在以后找到定义) - 注意&#之间的区别34;定义&#34;和&#34;声明&#34;
class Foo; // forward-declaration
Foo bar[N]; // usage
class Foo { }; // definition
移动声明,以便在使用之前定义类型Foo
:
class Foo {}; // definition
Foo bar[N]; // usage
此外,一些通用的C ++提示:
我建议仅在class
,字段,本地人中提供类型(struct
es,union
,typedef
,TitleCase
等)名称,(在我看来),参数和全局变量应该camelCase
,以便在代码涉及实例或静态成员并避免含糊不清时使其显而易见。
static Object ObjectArray[NUMBER_OF_OBJECTS];
变为
static Object objectArray[ NUMBER_OF_OBJECTS ];
(另外,我会避免使用名称Object
,因为许多库使用该名称。考虑Entity
或在其前面加上项目的短标识符,或将其移到命名空间内。)
代码中的表达式Object obj;
将在堆栈中创建Object
的实例,并在将其移动到数组中时执行复制。这是不必要的。如果您将代码更改为此代码,它将运行得更快,因为不会涉及复制:
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;
...但这是分裂的头发,优化编译器可能会为你做这件事。
答案 1 :(得分:1)
您的声明顺序很重要。编译器必须知道Object
的大小才能创建它们的数组。
首先移动Object
的定义是最简单的解决方案。