OpenGL glutInit 2

Sid Kraft 101 Reputation points
2026-08-01T02:09:07.6133333+00:00
#include <iostream>
#include<Gl\gl.h>
#include<GL\glut.h>
#include<GL\freeglut.h>
#include<cmath>
using namespace std;
int main()
{
    void display(void)
    {
        glClearColor(0.f, 0.f, 0.f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glFlush();
    }
    glutInit();
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
    return 0;
}

Here is a very simple set of codes to open a window in opengl code which I created to prove a point. It was said that there were other things going on in my large program that caused an error right after the void display(void) where the "{" was issuing an error saying "expected a ;" I still get this error even with this small subset of my larger program. So, in effect, there is some other issue causing the error, not sure what it is, Sid Kraft

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author

AI answer

2026-08-01T02:09:20.7+00:00

The error is caused by defining display inside main. In standard C++, a function definition cannot be nested inside another function, so the compiler reports a syntax error at the { after void display(void).

Move display out of main, then register it with glutDisplayFunc and glutIdleFunc.

#include <iostream>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <cmath>

using namespace std;

void display()
{
    glClearColor(0.f, 0.f, 0.f, 1.f);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
    return 0;
}

Also correct these points:

  1. glutInit is normally called with arguments: glutInit(&argc, argv);
  2. Header casing and paths should be consistent, for example #include <GL/gl.h> rather than mixing Gl and GL.
  3. glLoadIdentity and glFlush are valid OpenGL calls in the display function.

References:

AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

3 people found this answer helpful.

5 additional answers

Sort by: Newest
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Roma Khedr 5 Reputation points
    2026-08-03T01:55:35.0766667+00:00

    This code serves as the basic boilerplate template for opening a graphical window using OpenGL and GLUT in C++. It initializes the window, sets the background color to black, and continuously redraws it.🟢✅

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.