Already has a body

Sid Kraft 96 Reputation points
2026-08-01T13:52:18.02+00:00
//OpenGL functions, open a window and initialize
void display(void)
{
	glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glFlush();
}
void glutInit(int* argcp, char** argv)
{
	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
	glutInitWindowPosition(100, 200);
	glutInitWindowSize(800, 700);
	glutCreateWindow("This is a window title");
	glutDisplayFunc(display);
	glutIdleFunc(display);
	glutMainLoop();
}
int main()
Finally resolved the problem above by placing the display and glutInit functions outside of the main function, then executed ok. However, now getting an error C2084 says the glutInit function already has a body? Not sure what this means and why, please advise, 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.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,861 Reputation points
    2026-08-01T17:41:11.7466667+00:00

    C/C++ does not allow methods to be defined inside another method. but C++ has support for lambda, which are used to define anonymous functions with closure. you can declare a variable and define as a lambda inside a method.

    note: like fortran, C/C++ does not support calling a method that was not defined earlier in the source file. to get around this you use a forward declaration (function prototype). so the sample code could be written:

    
    void initWindow(int* argc, char** argv);
    void display(void);
    
    int main(int argc, char** argv)
    {
        initWindow(&argc, argv);
        glutMainLoop();
        return 0;
    }
    
    void initWindow(int* argc, char** argv)
    {
        glutInit(argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
        glutInitWindowPosition(100, 200);
        glutInitWindowSize(800, 700);
        glutCreateWindow("This is a window title");
        glutDisplayFunc(display);
        glutIdleFunc(display);
    }
    
    void display(void)
    {
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glFlush();
    }
    
    

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments

  2. AI answer

    2026-08-01T13:52:32.17+00:00

    C2084 means a function was defined more than once.

    In this code, the problem is the function name glutInit:

    void glutInit(int* argcp, char** argv)
    

    glutInit is already provided by the GLUT library, so writing a function with that same signature creates a second definition. The compiler reports that the function already has a body.

    Use a different name for the function that sets up the window, for example:

    void display(void)
    {
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glFlush();
    }
    
    void initWindow(int* argc, char** argv)
    {
        glutInit(argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
        glutInitWindowPosition(100, 200);
        glutInitWindowSize(800, 700);
        glutCreateWindow("This is a window title");
        glutDisplayFunc(display);
        glutIdleFunc(display);
    }
    
    int main(int argc, char** argv)
    {
        initWindow(&argc, argv);
        glutMainLoop();
        return 0;
    }
    

    Also make sure the needed OpenGL headers are included:

    • Core OpenGL: <GL\gl.h>
    • GLU: <GL\glu.h>
    • WGL and Windows support: Windows.h

    References:

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

    Was this answer helpful?

    1 person found this answer helpful.
  3. Danny Nguyen (WICLOUD CORPORATION) 8,195 Reputation points Microsoft External Staff Moderator
    2026-08-03T03:47:27.5166667+00:00

    Hi @Sid Kraft .

    I see the issue is an active compiler error C2084 stating that glutInit already has a body.

    The error C2084 means that a function has been defined more than once. glutInit is already a built-in function provided by the OpenGL GLUT utility library. When you define void glutInit(int* argcp, char** argv) in your code, the compiler stops you because it sees two definitions for the exact same function.

    To resolve this, you shouldn't create a custom function named glutInit. Instead, you should simply call the library's glutInit from within your main function (or create a custom function with a different name, like setupOpenGL) to initialize the window, like this:

    void display(void)
    {
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();
        glFlush();
    }
    
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
        glutInitWindowPosition(100, 200);
        glutInitWindowSize(800, 700);
        glutCreateWindow("This is a window title");
        glutDisplayFunc(display);
        glutIdleFunc(display);
        glutMainLoop();
        return 0;
    }
    

    If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback.

    Let me know if you need anything else.

    Thank you. 

    Was this answer helpful?


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.