错误:二进制“ operator *”类型为“ float”和“ float [0]”的无效操作数

时间:2018-09-27 05:01:16

标签: c++ graphics syntax-error runtime-error

所以我有用于图形管线的代码,但是由于某种原因我遇到了错误。

#include <GL/glut.h>
#include <stdio.h>
#include <unistd.h>

#define UpperBD 3
#define Xe 200
#define Ye  200
#define Ze  200
#define Rho  sqrt(sq(Xe) + sq(Ye) + sq(Ze))
#define PI 3.1415926
#define D_focal 20

typedef struct {
    float X[] = {};
    float Y[] = {};
    float Z[] = {};
} pworld;

typedef struct {
    float X[] = {};
    float Y[] = {};
    float Z[] = {};
} pviewer;

typedef struct{
    float X[] = {};
    float Y[] = {};
} pperspective;

void mydisplay()
{
    float p1x = 0.0f, p1y = 1.0f;    //the window coordinates (-1.0, 1.0)
    float p2x = 0.0f, p2y = -1.0f;
    float p3x = 1.0f, p3y = 0.0f;
    float p4x = -1.0f, p4y = 0.0f;

    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    /* Line starts */
    glBegin(GL_LINES);
    glVertex2f(p1x, p1y);
    glVertex2f(p2x, p2y);

    glVertex2f(p3x, p3y);
    glVertex2f(p4x, p4y);
    glEnd();

    /* Line Ends */

    // #define Pheta = PI/4.0;
    // #define Phi =
    /* World to viewer */
    pworld world[3];
    pviewer viewer[3];
    pperspective perspective[3];

    float sPheta = Ye / sqrt(sq(Xe) + sq(Ye));
    float cPheta = Xe / sqrt(sq(Xe) + sq(Ye));
    float sPhi = sqrt(sq(Xe) + sq(Ye)) / Rho;
    float cPhi = Ze / Rho;

    for(int i = 0; i <= UpperBD; i++)
    {
        viewer[i].X = -sPheta * world[i].X; + cPheta * world[i].y;
        viewer[i].Y = -cPheta * cPhi * world[i].X
            - cPhi * sPheta * world[i].Y
            + sPhi * world[i].Z;
        viewer[i].Z = -sPhi * cPheta * world[i].X
            - sPhi * cPheta * world[i].Y
            -cPheta * world[i].Z + Rho;
        perspective[i].X = (D_focal / viewer[i].Z) * viewer[i].X;
        perspective[i].Y = (D_focal / viewer[i].Z) * viewer[i].Y;
        cout << perspective[i].X << endl;
        cout << perspective[i].Y << endl;
    }

    glFlush();
    usleep(50);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("William");
    glutDisplayFunc(mydisplay);
    glutMainLoop();
}

该错误发生在viewer[i].X = -sPheta * world[i].X; + cPheta * world[i].y;及其for循环中其下方的行中。我不确定发生了什么。我正在尝试将float乘以float[],但不起作用。

1 个答案:

答案 0 :(得分:0)

typedef struct {
    float X[] = {};
    float Y[] = {};
    float Z[] = {};
    } pworld;

具有3个零元素(空)的C数组的结构。 -sPheta * world[i].X;正在将float(number)与空数组相乘-您想实现什么?我想您不需要这些[]。

但是,我认为禁止使用零元素C数组,但是它可以很好地编译。