【问题标题】:Error: invalid operands of types 'float' and 'float[0]' to binary 'operator*'错误:'float' 和 'float[0]' 类型的无效操作数到二进制 'operator*'
【发布时间】:2019-03-02 22:33:36
【问题描述】:

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

#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[],但它不起作用。

【问题讨论】:

  • 建议添加错误消息或您所看到的行为的描述。不相关:看看你是否可以用constexpr 变量替换那些defines。当某些标记意外地被文本替换时,这真的很糟糕。
  • 在声明viewer[i].X = -sPheta * world[i].X; + cPheta * world[i].y; viewer[i].Y 上,你在world[i].X 之后有一个错误的;

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


【解决方案1】:
typedef struct {
    float X[] = {};
    float Y[] = {};
    float Z[] = {};
    } pworld;

它是具有 3 个零元素(空)C 数组的结构。 -sPheta * world[i].X; 将 float(number) 与空数组相乘 - 你想达到什么目的?我猜你不需要这些 []。

但是,我认为零元素 C 数组是被禁止的,但它编译得很好。

【讨论】:

  • 不,这不是有效的 C。代码也不是用 C 编写的。
  • 这在 C++ 中也是无效的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多