c++ - 在 C++ 中获取广泛的类型错误

标签 c++ opengl glut

<分区>

这是我的程序的重要部分,我遇到了各种各样的类型错误。我正在尝试制作一个对象数组,以用作类似于 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;
}

最佳答案

C++ 的编译器执行单次传递(不同于 C# 和 Java 执行多次传递),这意味着编译器在使用类型之前必须知道它。

这里:

Foo bar[N];
class Foo { };

...不起作用,编译器看到数组定义 bar[N] 但不知道 Foo 是什么。

你有两个选择:

  1. 前向声明 Foo 而不定义它,因此编译器知道 Foo 存在(并且它知道它可以稍后找到定义)- 注意“定义”和“声明”的区别

    class Foo; // forward-declaration
    Foo bar[N]; // usage
    class Foo { }; // definition
    
  2. 四处移动声明,以便在使用之前定义类型 Foo:

    class Foo {}; // definition
    Foo bar[N]; // usage
    

此外,还有一些通用的 C++ 技巧:

我建议只给出类型 (classes, structs, unions, typedefs, etc ) 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;

...但这是 split 的头发,优化编译器可能会为您做这件事。

关于c++ - 在 C++ 中获取广泛的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27240797/

相关文章:

c++ - 链表复制构造函数的正确实现

python - 在 Python-OpenGL 中创建一个 GLUT 弹出菜单

c++ - 在OpenGL中用鼠标拾取在3D空间中拖动3D点的最佳方法?

Java2D OpenGL 图形加速不起作用

c++ - GLUT 和 freeglut includes 不能一起工作 (MinGW)

c++ - 删除四面体网格中内部元素的算法

c++ - 矩阵乘法性能,int 与 double

c++ - 如何在 CMake 中从 COMPILE_FLAGS 迁移到 target_compile_options?

c - GLUT 问题

c++/以特定方式在txt文件上追加数据