我试图在c ++中使用结构,但它不起作用

时间:2016-10-11 23:11:34

标签: c++ opengl struct operators

在这个方法中

void Sierpinski(GLintPoint points) {

    glClear(GL_COLOR_BUFFER_BIT);
    GLintPoint T[3] = {{10,10},{600,10},{300,600}};
    int index=rand()%3;
    GLintPoint point=points[index];
    drawDot(point.x, point.y);
    for (int i = 0;i<55000;i++) {
        index=rand()%3;
        point.x=(point.x+points[index].x)/2;
        point.y=(point.y+points[index].y)/2;
        drawDot(point.x,point.y);
    }
    glFlush();
}

使用我制作的结构

struct GLintPoint {
GLint x,y;
};

它表示存在错误,并且没有操作员&#34; []&#34;匹配这些操作数,运算符类型是GLintPoint [int]&#34;我尝试从点到点分配值。好吧,我确实使用右括号,它是一个int那里有什么问题?仅供参考,此代码用于绘制Sierpinski垫片,用户通过鼠标单击屏幕获得最初的3个点。万一你想在这里看到它是整个程序。

#include <windows.h>
#include <gl/Gl.h>
#include "glut.h"
#include <iostream>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;

struct GLintPoint {
    GLint x,y;
};

void display (void){
    glClear(GL_COLOR_BUFFER_BIT);
    //glColor3f(1.0,1.0,1.0);
    glFlush();

}

void drawDot( GLint x, GLint y)
{
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
}

void Sierpinski(GLintPoint points) {

    glClear(GL_COLOR_BUFFER_BIT);
    GLintPoint T[3] = {{10,10},{600,10},{300,600}};
    int index=rand()%3;
    GLintPoint point=points[index];
    drawDot(point.x, point.y);
    for (int i = 0;i<55000;i++) {
        index=rand()%3;
        point.x=(point.x+points[index].x)/2;
        point.y=(point.y+points[index].y)/2;
        drawDot(point.x,point.y);
    }
    glFlush();
}

void myMouse(int button, int state, int x, int y){
    static GLintPoint corner[2];
    static int numCorners = 0;
    if(state == GLUT_DOWN){
        if(button == GLUT_LEFT_BUTTON){
            corner[numCorners].x = x;
            corner[numCorners].y = screenHeight - y;
            if(++numCorners ==2 ){
                glRecti(corner[0].x, corner[0].y, corner[1].x,    corner[1].y);
               numCorners = 0;
            glFlush();
        }
    }
        else if(button == GLUT_RIGHT_BUTTON){
            glClear(GL_COLOR_BUFFER_BIT);
            glFlush();
        }   
}
}

void myInit() {

    glClearColor(1.0,1.0,1.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(0.0f,0.0f,0.0f);
   glPointSize(2.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}



void main (int argc, char** argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(screenWidth, screenHeight);
    glutInitWindowPosition(100,150);
    glutCreateWindow("mouse dots");
    glutDisplayFunc(display);
    glutPostRedisplay();
    glutMouseFunc(myMouse);
   myInit();
   glutMainLoop();
}

1 个答案:

答案 0 :(得分:4)

我从函数参数points的名称推断,你打算让函数接受一个点数组。但它实际上只是写了一个。

因此,当您编写points[index]时,编译器正在operator[]结构中查找GLintPoint(并且没有#)}。

如果您更改函数原型以获取GLintPoint的数组,我认为您会有更好的运气。